There is a two-dimensional array. I want to retrieve a set of keys and calculate the sum.

Source: Internet
Author: User
There is a two-dimensional array. I want to retrieve a set of keys and calculate the sum. this is a two-dimensional array. I want to go to the total set and calculate the total value, how can I implement array (& nbsp; [0] & nbsp; & gt; & nbsp; Array & nbsp; (& nbsp; [id] & nbsp; & there is a two-dimensional Array. I want to retrieve a set of keys and calculate the sum.
This is a two-dimensional array. I want to go to the total set and calculate the total value. how can I implement this?

Array (
[0] => Array (
[Id] => 5
[Title] => Sept. grass 8 times gel cream 50g moisturizing whitening cream lotion genuine skin care products for men and women
[Images] => uploadfile/goods/2015-01/1420774763. jpg
[Price] = & gt; 39.0
[Num] => 1
[Total] => 39
[Wlmoney] => 0
[Url] => http: // localhost/dkyq/goods. php? Id = 12)


[1] => Array (
[Id] => 4
[Title] => skin lotion, genuine water source cleanser, facial cleanser, 75 ml cleanser, cleansing, refreshing, moisturizing
[Images] => uploadfile/goods/2015-01/1420773795. jpg
[Price] = & gt; 12.0
[Num] => 1
[Total] => 12
[Wlmoney] => 0
[Url] => http: // localhost/dkyq/goods. php? Id = 11
)
);

------ Solution ----------------------
$ Ar = array (
0 => array (
'Id' => '5 ',
'Title' => '50g moisturizing whitening cream lotion male and female genuine skin care s ',
'Images' => 'uploadfile/goods/2015-01/1420774763. jpg ',
'Price' => '39. 0 ',
'Num' => '1 ',
'Total' => '39 ',
'Wlmoney' => '0 ',
'URL' => 'http: // localhost/dkyq/goods. php? Id = 12 ',
),
1 => array (
'Id' => '4 ',
'Title' => 'Skin lotion genuine water source cleanser 75 ml cleanser cleansing and moisturizing ',
'Images' => 'uploadfile/goods/2015-01/1420773795. jpg ',
'Price' => '12. 0 ',
'Num' => '1 ',
'Total' => '12 ',
'Wlmoney' => '0 ',
'URL' => 'http: // localhost/dkyq/goods. php? Id = 11 ',
),
);

$ Total = 0;
Foreach ($ ar as $ r) $ total + = $ r ['total'];
Echo $ total; // 51

------ Solution ----------------------

$ Arr = array (
Array (
'Id' => '5 ',
'Title' => '50g moisturizing whitening cream lotion male and female genuine skin care s ',
'Images' => 'uploadfile/goods/2015-01/1420774763. jpg ',
'Price' => '39. 0 ',
'Num' => '1 ',
'Total' => '39 ',
'Wlmoney' => '0 ',
'URL' => 'http: // localhost/dkyq/goods. php? Id = 12 ',
),
Array (
'Id' => '4 ',
'Title' => 'Skin lotion genuine water source cleanser 75 ml cleanser cleansing and moisturizing ',
'Images' => 'uploadfile/goods/2015-01/1420773795. jpg ',
'Price' => '12. 0 ',
'Num' => '1 ',
'Total' => '12 ',
'Wlmoney' => '0 ',
'URL' => 'http: // localhost/dkyq/goods. php? Id = 11 ',
)
);
$ Total = 0;
Foreach ($ arr as $ v ){
$ Total + = $ v ['total'];
}
Echo $ total;


Total = 51

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.