0
我有一个包含数组对象数组的多维结果,需要将此结果合并到单个数组中,其唯一值为content
,总和为total
。如下所示的结果。帮助肯定是赞赏的。相同的内容值需要在多维数组中求和 - 使用PHP
结果集
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;
});