: This article mainly introduces the value accumulation function of the same key in the PHP multi-dimensional array. if you are interested in the PHP Tutorial, refer to it. Function
Function array_value_sum () {$ res = array (); foreach (func_get_args () as $ arr) {foreach ($ arr as $ k => $ v) {if (! Isset ($ res [$ k]) {$ res [$ k] = $ v;} else {$ res [$ k] + = $ v ;}}} return $ res ;}
Instance:
$ Arr1 = array (311 => 1,312 => 2,314 => 2); $ arr2 = array (311 => 2,312 => 2,313 => 5,314 => 9 ); $ arr3 = array (314 => 10); $ newArr = array_value_sum ($ arr1, $ arr2, $ arr3); print_r ($ newArr );
Output:
Array ([311] => 3 [312] => 4 [314] => 21 [313] => 5)
The above describes the value accumulation function of the same key in the PHP multi-dimensional array, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.