php计算每10个数组元素的总数

时间:2017-10-10 07:00:15

标签: php arrays sum

Array ( 
    [0] => Array ( 
        [amount_paid] => 2050.00 
    )
    [1] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [2] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [3] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [4] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [5] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [6] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [7] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [8] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [9] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [10] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [11] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [12] => Array ( 
        [amount_paid] => 2050.00 
    ) 
    [13] => Array ( 
        [amount_paid] => 2050.00 
    ) 

) 

我在php中有这个数组的例子。我想总结每10个付费金额。

  

示例是和索引0-9,10-19等等

我尝试的只是前10个,我不能考虑如何处理每10个动态数字

for ($i = 0; $i < count($json); $i++) {
    if ($i < 10) {
        $subtotalamount += floatval($json[$i]['amount_paid']);
    }
}

5 个答案:

答案 0 :(得分:2)

易于理解,您可以使用array_chunk()array_sum()array_column(),如下所示: -

$new_array = array_chunk($array,10);
$sub_total_array = [];
foreach($new_array as $array){
   $sub_total_array[] = array_sum(array_column($array,'amount_paid'));
}
print_r($sub_total_array);

https://eval.in/876914

答案 1 :(得分:1)

使用rangearray_slicearray_column函数的简短解决方案:

// $arr is your initial array
$sub_totals = [];
foreach(range(0, count($arr), 10) as $k){
    $sub_totals[] = array_sum(array_column(array_slice($arr, $k, 10), 'amount_paid'));
}
  • range(0, count($arr), 10) - 生成包含一系列元素的数组。它将是[0, 10, 20]。这些元素是每个10大小序列的边界

  • array_slice($arr, $k, 10) - 每个下一个边界$k用于从初始数组$arr中提取下一个10大小的序列/切片

答案 2 :(得分:0)

$ret = array();
$subtotalamount = 0;
for ($i = 0; $i < count($json); $i++) {
    $subtotalamount += floatval($json[$i]['amount_paid']);
    if ($i % 10 == 9) {
        $ret[] = $subtotalamount;
        $subtotalamount = 0;
    }
}
if ($subtotalamount > 0) {
    $ret[] = $subtotalamount;
}

答案 3 :(得分:0)

试试这个,

$sumofall = array();
for ($i = 0; $i < count($json); $i++) {
    if ($i % 10 == 0) {       
        $sumofeach = 0 
        for ($j=$i-10;$j<$i-1;$j++) {
            $sumofeach += floatval($json[$i]['amount_paid']);
        $sumofall[] = $sumofeach
    }
}

答案 4 :(得分:0)

$a = array (0 => array ( 'amount_paid' => 2050.00 ), 1 => array ( 'amount_paid' => 2050.00 ) , 2 => array ( 'amount_paid' => 2050.00 ) , 3 => array ( 'amount_paid' => 2050.00 ) , 4 => array ( 'amount_paid' => 2050.00 ) , 5 => array ( 'amount_paid' => 2050.00 ) , 6 => array ( 'amount_paid' => 2050.00 ) , 7 => array ( 'amount_paid' => 2050.00 ) , 8 => array ( 'amount_paid' => 2050.00 ) , 9 => array ( 'amount_paid' => 2050.00 ) , 10 => array ( 'amount_paid' => 2050.00 ) , 11 => array ( 'amount_paid' => 2050.00 ) , 12 => array ( 'amount_paid' => 2050.00 ) , 13 => array ( 'amount_paid' => 2050.00 ) ) ; 

$b = array_column($a,'amount_paid');

$c = array_chunk($b,10);

$sum = [];
foreach($c as $arr){
    $sum[] = array_sum($arr);
}

echo "sum = ";
print_r($sum);