多个数组形成另一个数组的有关问题

WBOY
Lepaskan: 2016-06-13 11:53:31
asal
1027 orang telah melayarinya

多个数组形成另一个数组的问题
程序中形成了以下的数组(程序的中的key是动态的,这里写死了)

Array<br />(<br />    [Category] => TOTAL<br />    [January2014_Number_stores] => 0<br />    [February2014_Number_stores] => 0<br />    [March2014_Number_stores] => 0<br />)
Salin selepas log masuk

又有数组
Array<br />(<br />    [January2014_Number_stores] => 155<br />    [February2014_Number_stores] => 155<br />    [March2014_Number_stores] => 155<br />)<br />Array<br />(<br />    [January2014_Number_stores] => 126<br />    [February2014_Number_stores] => 126<br />    [March2014_Number_stores] => 126<br />)<br />Array<br />(<br />    [January2014_Number_stores] => 37<br />    [February2014_Number_stores] => 37<br />    [March2014_Number_stores] => 37<br />)<br />Array<br />(<br />    [January2014_Number_stores] => 9<br />    [February2014_Number_stores] => 9<br />    [March2014_Number_stores] => 9<br />)<br />Array<br />(<br />    [January2014_Number_stores] => 4<br />    [February2014_Number_stores] => 4<br />    [March2014_Number_stores] => 4<br />)
Salin selepas log masuk

现在要把第二组中的数组中key值相同的相加求和后赋值给第一个数组,该怎么写? 如果key值是动态有规律的该怎么写?
------解决方案--------------------
$a = array(<br />  'Category' => 'TOTAL',<br />  'January2014_Number_stores' => 0,<br />  'February2014_Number_stores' => 0,<br />  'March2014_Number_stores' => 0,<br />);<br /><br />$b = array(<br />  array(<br />    'January2014_Number_stores' => 155,<br />    'February2014_Number_stores' => 155,<br />    'March2014_Number_stores' => 155,<br />  ),<br />  array(<br />    'January2014_Number_stores' => 126,<br />    'February2014_Number_stores' => 126,<br />    'March2014_Number_stores' => 126,<br />  ),<br />  array(<br />    'January2014_Number_stores' => 37,<br />    'February2014_Number_stores' => 37,<br />    'March2014_Number_stores' => 37,<br />  ),<br />  array(<br />    'January2014_Number_stores' => 9,<br />    'February2014_Number_stores' => 9,<br />    'March2014_Number_stores' => 9,<br />  ),<br />  array(<br />    'January2014_Number_stores' => 4,<br />    'February2014_Number_stores' => 4,<br />    'March2014_Number_stores' => 4,<br />  ),<br />);<br />foreach($b as $r) {<br />  foreach($r as $k=>$v) {<br />    if(! isset($a[$k])) $a[$k] = 0;<br />    $a[$k] += $v;<br />  }<br />}<br />print_r($a);
Salin selepas log masuk
Array
(
    [Category] => TOTAL
    [January2014_Number_stores] => 331
    [February2014_Number_stores] => 331
    [March2014_Number_stores] => 331
)

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan