php built-in function array_sum() function returns the sum of all values in the array, and can only return the sum of one-dimensional arrays;
To calculate the sum of all values in a multi-dimensional array, a custom function is required;
1function get_sum($array) { 2$num = 0; 3foreach($arrayas$k => $v) { 4if(is_array($v)) { 5$num += get_sum($v); 6 } 7 } 8return$num + array_sum($array); 9 }
10 get_sum($array);
The above introduces PHP to calculate the sum of all values in a multi-dimensional array, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.