私は配列オブジェクトの数を含む配列の多次元結果を持っており、この結果を一意の値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;
});