Rumah > pembangunan bahagian belakang > tutorial php > 有一个二维数组,我想取出其中一个key的集合,并计算总和

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

WBOY
Lepaskan: 2016-06-23 13:41:34
asal
974 orang telah melayarinya

这是二维数组,我想去到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 		) );
Salin selepas log masuk


回复讨论(解决方案)

$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
Salin selepas log masuk

$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;
Salin selepas log masuk


total = 51

不好意思了,因为版主回答的比较早,后面我只能少给点了,希望不要介意呀

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan