多维数组中相同元素相加问题
<br />$a = array(<br /> <br /> 0 => array(<br /><br /> 0=> array(<br /> <br /> "date" => "201201",<br /> "area"=> array(<br /> 12 => array(<br /> click => "3",<br /> traffic => "30"<br /> ) <br /> )<br /> ), <br /><br /> 1=> array(<br /> <br /> "date" => "201202",<br /> "area"=> array(<br /> 12 => array(<br /> click => "8",<br /> traffic => "40"<br /> ),<br /> 8=> array(<br /> click => "5",<br /> traffic => "50"<br /> )<br /> )<br /> ) <br /> <br /> ),<br /><br /> 1 => array(<br /> <br /> 0=> array(<br /> <br /> "date" => "201201",<br /> "area"=> array(<br /> 12 => array(<br /> click => "9",<br /> traffic => "20"<br /> ),<br /> 8=> array(<br /> click => "6",<br /> traffic => "60"<br /> )<br /> )<br /> ),<br /> <br /> 1=> array(<br /> <br /> "date" => "201202",<br /> "area" => NULL<br /> )<br /> ),<br /> <br /> <br /> );<br /><br />
$t = array();<br />foreach ($a as $k=>$v)<br />{<br /> foreach ($v as $k1=>$v1)<br /> {<br /> $t[0][$k1]['date'] = $v1['date'];<br /> foreach ($v1['area'] as $k2=>$v2)<br /> {<br /> $t[0][$k1]['area'][$k2]['click'] += $v2['click'];<br /> $t[0][$k1]['area'][$k2]['traffic'] += $v2['traffic'];<br /> }<br /> }<br />}<br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($t);<br />echo "<pre class="brush:php;toolbar:false">";
<br>Array<br>(<br> [0] => Array<br> (<br> [0] => Array<br> (<br> [date] => 201201<br> [area] => Array<br> (<br> [12] => Array<br> (<br> [click] => 12<div class="clear"> </div>