欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

有一个二维数组,小弟我想取出其中一个key的集合,并计算总和

程序员文章站 2022-06-09 12:26:33
...
有一个二维数组,我想取出其中一个key的集合,并计算总和
这是二维数组,我想去到total的集合,并计算total的总值,请问怎么实现

array(
[0] => Array (
[id] => 5
[title] => 相宜本草八倍凝水霜50g 保湿补水滋润美白面霜乳液男女正品护肤品
[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] => 美肤宝正品 水份源洁面膏洁面乳75ml 洗面奶清洁爽肤 补水保湿
[images] => uploadfile/goods/2015-01/1420773795.jpg
[price] => 12.0
[num] => 1
[total] => 12
[wlmoney] => 0
[url] => http://localhost/dkyq/goods.php?id=11
)
);

------解决思路----------------------
$ar = array (
0 => array (
'id' => '5',
'title' => '相宜本草八倍凝水霜50g 保湿补水滋润美白面霜乳液男女正品护肤品',
'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' => '美肤宝正品 水份源洁面膏洁面乳75ml 洗面奶清洁爽肤 补水保湿',
'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

------解决思路----------------------

$arr = array(
array(
'id' => '5',
'title' => '相宜本草八倍凝水霜50g 保湿补水滋润美白面霜乳液男女正品护肤品',
'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' => '美肤宝正品 水份源洁面膏洁面乳75ml 洗面奶清洁爽肤 补水保湿',
'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
有一个二维数组,小弟我想取出其中一个key的集合,并计算总和

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频