###要件
DBから取得したデータで、集計の際に必要な以下のような配列があり、配列の中身を追加しながらマージをしたいと思っております。
バージョン
CentOS 6.8
php : 5.6
php
1[0]=> 2 array(6) { 3 ["call_count"]=>string(2) "22" 4 ["duration"]=>string(8) "00:00:15" 5 ["call_status"]=> 6 array(5) { 7 ["断り"]=>int(5) 8 ["不在"]=>int(4) 9 ["留守"]=>int(5) 10 ["断り(他)"]=>int(7) 11 [""]=>int(1) 12 } 13 } 14 [1]=> 15 array(5) { 16 ["call_count"]=>string(1) "9" 17 ["duration"]=>string(8) "00:00:30" 18 ["call_status"]=> 19 array(4) { 20 [""]=>int(1) 21 ["資料送付"]=>int(2) 22 ["断り"]=>int(5) 23 ["不在"]=>int(1) 24 } 25 }
###期待する結果
php
1[0]=> 2 array(6) { 3 ["call_count"]=>string(2) "31" 4 ["duration"]=>string(8) "00:00:45" 5 ["call_status"]=> 6 array(5) { 7 ["断り"]=>int(10) 8 ["不在"]=>int(5) 9 ["留守"]=>int(5) 10 ["断り(他)"]=>int(7) 11 ["資料送付"]=>int(2) 12 [""]=>int(2) 13 } 14 }
###やってみた事
+演算子で一次の配列は問題なく追加出来たのですが、これだと2次元になるcall_countの配列がどうしても値まで追加しながらが実現出来ず、方法がわからないままでいます。
php
1foreach($data as $key => $val) { 2 if($key < 0) { 3 $data[0]['call_count'] += $val['call_count']; 4 $data[0]['duration'] += $val['duration']; 5 $data[0]['call_status'] += $val['call_status']; 6 } 7 }
回答1件
あなたの回答
tips
プレビュー