【问题标题】:Same content value need to sum in- multidimensional array - using PHP相同的内容值需要对多维数组求和 - 使用 PHP
【发布时间】:2016-08-28 04:07:58
【问题描述】:

我有一个包含多个数组对象的数组的多维结果,需要将此结果合并到唯一值为contenttotal 之和的数组的单个实例中。就像下面想要的结果。绝对感谢您的帮助。

结果集

Array
(
    [0] => Array
        (
            [response_id] => 23598
            [choice_question_detail] => Array
                (
                    [0] => Array
                        (
                            [content] => How old are your.
                            [total] => 5
                        )

                    [1] => Array
                        (
                            [content] => Stadium.
                            [total] => 4
                        )
          ),
      [1] => Array
        (
            [response_id] => 23599
            [choice_question_detail] => Array
                (
                    [0] => Array
                        (
                            [content] => How old are your.
                            [total] => 2
                        )

                    [1] => Array
                        (
                            [content] => Stadium.
                            [total] => 1
                        )
          )    
 ) 

期望的结果

Array
(
    [0] => Array
        (
            [content] => How old are your.
            [total] => 7
        )

    [1] => Array
        (
            [content] => Stadium.
            [total] => 5
        )
) 

我当前的实现尝试做这样的事情:

$sum = array_reduce($data, function ($a, $b) {
    isset($a[$b['choice_question_detail']]) ? $a[$b['choice_question_detail']]['total'] += $b['total'] : $a[$b['total']] = $b;  
return $a;
});

【问题讨论】:

    标签: php arrays multidimensional-array merge sum


    【解决方案1】:

    循环是这样的......有点讨厌,但它应该可以工作。

    当然还有很大的优化空间,如何改进就看你自己了。

    $out = array();
    
        foreach($var as $row) {
            foreach($row['choice_question_detail'] as $detail) {
                $flag = false;
                $numkey = -1;
                foreach($out as $key => $x) {
                    if($x['content'] == $detail['content']) {
                        $flag = true;
                        $numkey = $key;
                    }
                }
                if(!$flag) {
                    $out[] = array(
                         'content' => $detail['content'],
                         'total' => $detail['total']          
                    );
                } else {
                    $out[$numkey]['total'] = $out[$numkey]['total'] + $detail['total'];
                }
            }
        }
    

    输出是这样的

    array (size=2)
       0 => array (size=2)
              'content' => string 'How old are your.' (length=17)
              'total' => int 7
       1 => array (size=2)
             'content' => string 'What is your name.' (length=18)
             'total' => int 5
    

    【讨论】:

      【解决方案2】:

      没有本地方法可以做到这一点,自己写吧。

      $merged = array();
      foreach($responses["choice_question_detail"] as $question){
          if($merged_q=$merged[$question["content"]]){
              $merged_q["total"] += $question["total"];
          }else{
              $merged[$question["content"]] = $qeustion;
          }
      }
      $desired = array_values($merged);
      

      【讨论】:

        猜你喜欢
        • 2013-02-24
        • 2020-07-08
        • 2023-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-29
        • 1970-01-01
        • 2016-09-16
        相关资源
        最近更新 更多