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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:14:05
Original
1056 people have browsed it

有一个二维数组,我想取出其中一个key的集合,并计算总和
这是二维数组,我想去到total的集合,并计算total的总值,请问怎么实现

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

------解决思路----------------------
$ar = array (<br />  0 => array (<br />    'id' => '5',<br />    'title' => '相宜本草八倍凝水霜50g 保湿补水滋润美白面霜乳液男女正品护肤品',<br />    'images' => 'uploadfile/goods/2015-01/1420774763.jpg',<br />    'price' => '39.0',<br />    'num' => '1',<br />    'total' => '39',<br />    'wlmoney' => '0',<br />    'url' => 'http://localhost/dkyq/goods.php?id=12',<br />  ),<br />  1 => array (<br />    'id' => '4',<br />    'title' => '美肤宝正品 水份源洁面膏洁面乳75ml 洗面奶清洁爽肤 补水保湿',<br />    'images' => 'uploadfile/goods/2015-01/1420773795.jpg',<br />    'price' => '12.0',<br />    'num' => '1',<br />    'total' => '12',<br />    'wlmoney' => '0',<br />    'url' => 'http://localhost/dkyq/goods.php?id=11',<br />  ),<br />);<br /><br />$total = 0;<br />foreach($ar as $r) $total += $r['total'];<br />echo $total; //51
Copy after login

------解决思路----------------------
<br />$arr = array(<br />   array(<br />    'id' => '5',<br />    'title' => '相宜本草八倍凝水霜50g 保湿补水滋润美白面霜乳液男女正品护肤品',<br />    'images' => 'uploadfile/goods/2015-01/1420774763.jpg',<br />    'price' => '39.0',<br />    'num' => '1',<br />    'total' => '39',<br />    'wlmoney' => '0',<br />    'url' => 'http://localhost/dkyq/goods.php?id=12',<br />  ),<br />  array(<br />    'id' => '4',<br />    'title' => '美肤宝正品 水份源洁面膏洁面乳75ml 洗面奶清洁爽肤 补水保湿',<br />    'images' => 'uploadfile/goods/2015-01/1420773795.jpg',<br />    'price' => '12.0',<br />    'num' => '1',<br />    'total' => '12',<br />    'wlmoney' => '0',<br />    'url' => 'http://localhost/dkyq/goods.php?id=11',<br />  )<br />);<br />$total = 0;<br />foreach($arr as $v){<br />    $total += $v['total'];<br />}<br />echo $total;<br />
Copy after login


total = 51

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template