【发布时间】:2016-08-28 04:07:58
【问题描述】:
我有一个包含多个数组对象的数组的多维结果,需要将此结果合并到唯一值为content 和total 之和的数组的单个实例中。就像下面想要的结果。绝对感谢您的帮助。
结果集
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