Php calculates the sum of all values in a multi-dimensional array. multi-dimensional array _ PHP Tutorial

Source: Internet
Author: User
Php calculates the sum of all values in a multi-dimensional array. Php calculates the sum of all values in the multi-dimensional array. the array_sum () function of the php built-in function of the multi-dimensional array returns the sum of all values in the array. only the sum of one-dimensional arrays can be returned; calculate the sum of all values in the multi-dimensional array by using the multi-dimensional php.

The php built-in function array_sum () returns the sum of all values in the array. only the sum of one-dimensional arrays can be returned;

To calculate the sum of all values of a multi-dimensional array, you must use a custom function;

 
1 function get_sum ($ array) {2 $ num = 0; 3 foreach ($ array as $ k => $ v) {4 if (is_array ($ v )) {5 $ num + = get_sum ($ v); 6} 7} 8 return $ num + array_sum ($ array); 9}
10 get_sum ($ array );
 

 

The http://www.bkjia.com/PHPjc/1018253.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1018253.htmlTechArticlephp calculates the sum of all values in the multi-dimensional array, the multi-dimensional array php built-in function array_sum () function returns the sum of all values in the array, can only return the sum of one-dimensional array; calculate the number of multi-dimensional...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.