简体   繁体   English

以 10 行为一组对 2d 数组进行分组,然后对每批中的值求和

[英]Group 2d array in batches of 10 rows then sum the values in each batch

I have an array of rows which need to be batched into groups of ten rows then their values summed.我有一个行数组,需要将它们分成十行一组,然后将它们的值相加。

Sample array with 13 rows:具有 13 行的示例数组:

[
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
]

Desired result from sample array:示例数组的期望结果:

[20500.0, 8200.0]

The above represents the sum of the first 10 rows, then the sum of the remaining 3 rows.上面表示前 10 行的总和,然后是其余 3 行的总和。

What I tried is only for first 10 and I can't think of how to handle the data for every 10 with dynamic number of rows.我尝试的只是前 10 个,我想不出如何处理每 10 个具有动态行数的数据。

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

Easy to understand,You can use array_chunk() , array_sum() , array_column() like below:- 易于理解,您可以使用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 https://eval.in/876914

Short solution with range , array_slice and array_column functions: 使用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) - generate an array containing a range of elements. range(0, count($arr), 10) -生成包含一系列元素的数组。 It would be as [0, 10, 20] . 它将是[0, 10, 20] Those elements are boundaries for each 10-sized sequence 这些元素是每10个大小的序列的边界

  • array_slice($arr, $k, 10) - each next boundary $k is used for extracting next 10-sized sequence/slice from the initial array $arr array_slice($arr, $k, 10) -每个下一个边界$k用于从初始数组$arr提取下一个10个大小的序列/切片

$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;
}

try this, 尝试这个,

$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
    }
}
$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);

The functional-style equivalent of @Anant's answer is to use array_map() .与@Anant 的答案等效的功能样式是使用array_map()

Code: ( Demo )代码:(演示

var_export(
    array_map(
        fn($set) => array_sum(array_column($set, 'amount_paid')),
        array_chunk($array, 10)
    )
);

In a classic loop, access the column values and push the summed values into their respective groups using the truncated dividend when dividing the first level indexes by 10.在经典循环中,访问列值并在将第一级索引除以 10 时使用截断的被除数将求和值推入各自的组。

Code: ( Demo )代码:(演示

$result = [];
foreach ($array as $i => $row) {
    $group = intdiv($i, 10);
    $result[$group] = ($result[$group] ?? 0) + $row['amount_paid'];
}
var_export($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM