【问题标题】:Multiarray which need to be transform with the help of some business rules which are sum,unique,add in PHP需要借助一些业务规则进行转换的多数组,这些规则是总和,唯一的,在 PHP 中添加
【发布时间】:2019-05-03 09:54:42
【问题描述】:

我有多个数组,需要借助 sum、unique、add 等一些业务规则根据以下所需结果进行转换。我需要你的支持来解决它。

非常感谢您的帮助

$arr = array (
    'AMXB 5321'  => array(
    array("course_title_code" => "AMB 5321",
          "content" => "Course",
          "total" => "303",
          "count" => "85",
          "ranking" => array(array(1,2,3,4,5),array(1,2,3,4,5)),
          "frequency" => array(array(2,3,9,13,17),array(6,3,13,14,5))),
    array("course_title_code" => "AMB 5321",
          "content" => "Succeed in the course",
          "total" => "300",
          "count" => "85",
          "ranking" => array(array(5),array(3,4,5)),
          "frequency" => array(array(1),array(3,3,9))
         ),
    array("course_title_code" => "AMB 5321",
          "content" => "conduct of the course",
          "total" => "304",
          "count" => "85",
          "ranking" => array(array(5),array(1,2,3,4,5)),
          "frequency" => array(array(1),array(1,1,2,3,8))
         )
));
echo "<pre>";
print_r($arr);

想要的结果

Array
(
    [AMXB 5321] => Array
        (
            [0] => Array
                (
                    [course_title_code] => AMB 5321
                    [content] => Course
                    [total] => 303
                    [count] => 85
                    [ranking] => 1,2,3,4,5
                    [frequency] => 8,6,22,27,22
                )

            [1] => Array
                (
                    [course_title_code] => AMB 5321
                    [content] => Succeed in the course
                    [total] => 300
                    [count] => 85
                    [ranking] => 1,2,3,4,5
                    [frequency] => 0,0,3,3,10
                )

            [2] => Array
                (
                    [course_title_code] => AMB 5321
                    [content] => conduct of the course
                    [total] => 304
                    [count] => 85
                    [ranking] => 1,2,3,4,5
                    [frequency] => 1,1,2,3,9
                )

        )

)

附加的快照会更清楚我的需求状态

【问题讨论】:

  • 你遇到了什么问题?你有什么问题吗?
  • @Dharman 如何使用多数组构建期望结果和逻辑
  • 这看起来更像是向提出要求的人提出的问题。我们不知道必须对您的数据应用哪些转换才能达到预期的结果。
  • 希望得到正确答案

标签: php arrays multidimensional-array logic


【解决方案1】:

您可以使用array_walk 来解决此问题

array_walk($arr, function(&$v,$k){
    foreach($v as $key => &$s){
        $s['ranking'] = implode(',',range(1,5));
        foreach($s['frequency'] as $key => &$value){
            $temp   = $value;
            $value = (count($value) == 5) ? $value : array_merge(array_fill(0, 5 - count($value), 0), $temp);
            if($key == 1){
                for($i=0;$i<count($value);$i++){
                    $value[$i] += $s['frequency'][$key-1][$i];
                } 
                $temFormat = implode(',',$s['frequency'][1]);
            } 
        }
        unset($s['frequency']); 
        $s['frequency'] = $temFormat;
    }
});

【讨论】:

  • 感谢您的宝贵回答,它对我有用
  • @QueryMaster 如果解决了问题,请接受答案
【解决方案2】:

在分析您的问题并模块化如下,

$count = 5; // at your will change it
array_walk($arr, function($item, $key) use(&$result, $count){
    foreach($item as $k => $v){
        // ... its splat operator which merge child arrays
        $temp = array_unique(array_merge(array_merge(...$v['ranking']),range(1,$count)));
        // sort as we need 1,2,3,4,5 or upto $count
        sort($temp);
        $result[$key][$k] = $v; // assigning all other values to result
        $result[$key][$k]['ranking'] = implode(",",$temp);
        foreach($v['frequency'] as &$v1){
            $v1 = array_pad($v1, -$count,0); // padding from left to right with zeros
        }
        // splat operator https://www.php.net/manual/en/migration56.new-features.php
        $result[$key][$k]['frequency'] = implode(",",array_map(function (...$arrs) {return array_sum($arrs);}, ...$v['frequency']));
    }
});

Demo.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多