有一個二維數組,我想取出其中一個key的集合,並計算總和

來源:互聯網
上載者:User
這是二維數組,我想去到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

不好意思了,因為版主回答的比較早,後面我只能少給點了,希望不要介意呀

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.