-1

如何获得所有数组的 total_qty_ordered 的加法?

这可以是任意数量的数组

       Array ( [0] => Array ( [total_qty_ordered] => 1.0000 [1] => ) [1] => Array
     ( [total_qty_ordered] => 1.0000 [1] => ) [2] => Array ( [total_qty_ordered] => 1.0000
 [1] =>
     ) [3] => Array ( [total_qty_ordered] => 1.0000 [1] => ) [4] => Array ( 
[total_qty_ordered] =>
     1.0000 [1] => ) [5] => Array ( [total_qty_ordered] => 1.0000 [1] => ) [6] => Array (

    [total_qty_ordered] => 1.0000 [1] => ) [7] => Array ( [total_qty_ordered] => 1.0000 [1]
 => )
     [8] => Array ( [total_qty_ordered] => 1.0000 [1] => ) [9] => Array ( 
[total_qty_ordered] => 
    2.0000 [1] => ) [10] => Array ( [total_qty_ordered] => 1.0000 [1] => ) [11] => Array (
 [total_qty_ordered] => 1.0000 [1] => ) )
4

2 回答 2

2
$sum = array_sum(array_map(function($item){
  return $item['total_qty_ordered'];
}, $array));

但是如果你的数组结构真的是这样(第二个值真的是空的),你也可以这样做:

$sum = array_sum(call_user_func_array('array_merge',
            array_map('array_values', $array)));
于 2013-05-13T01:44:02.690 回答
0

在 PHP 5.5 中,您可以执行以下操作:

 $sum = array_sum(array_col('total_qty_ordered', $array));
于 2013-05-13T01:47:31.627 回答