Dragon
主机之家测评主机之家测评  2019-08-26 09:21 主机之家测评 隐藏边栏 |   抢沙发  15 
文章评分 0 次,平均分 0.0

数组示例:

  [0] => array(3) {     ["id"] => string(1 "1"     ["name"] => string(4) "jhon"     ["amount"] => string(3) "850"    }  [1] => array(3) {     ["id"] => string(1) "2"     ["name"] => string() "david"     ["amount"] => string(4) "1200"    }  [2] => array(3) {     ["id"] => string(1) "3"     ["name"] => string(4) "lucy"     ["amount"] => string(3) "200"    }

需求说明:计算数组中元素的 amount 项的和。

一:

  $total = array_sum(array_map(function($val){return $val['amount'];}, $arr));

方法二:

  $total = 0;  foreach($arr as $item){      $total += (int) $item['amount'];  }

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

发表评论

扫一扫二维码分享