php中计算二维数组中某一元素之和

时间:2022-03-14 19:00:52
[0] => array(5) {
      ["id"] => string(2) "11"
      ["name"] => string(5) "1.jpg"
      ["suffix"] => string(3) "jpg"
      ["url"] => string(29) "./Uploads/1/5292f55d208e8.jpg"
      ["size"] => string(6) "715895"
    }
    [1] => array(5) {
      ["id"] => string(2) "12"
      ["name"] => string(22) "minisite数据库.docx"
      ["suffix"] => string(4) "docx"
      ["url"] => string(30) "./Uploads/1/5292f5606a52c.docx"
      ["size"] => string(5) "16667"
    }

我要计算size的和

(1)

echo array_sum ( array_map (create_function( '$val' , 'return $val["size"];' ), $arr ));
或者
echo array_sum ( array_map ( function ( $val ){ return $val [ 'size' ];}, $arr ));
(2)
$sum = 0;
foreach ( $arr as $item ){
   $sum += (int) $item [ 'size' ];
}