<?php
$line_items = [
['price' => 41.99, 'id' => 12, 'quantity' => 1],
['price' => 155.95, 'id' => 11, 'quantity' => 1],
['price' => 21, 'id' => 13, 'quantity' => 1]
];
$price_array = [
197.94,
21.00
];
$result = [];
foreach ($line_items as $row) {
$lastKey = array_key_last($result);
if (
!$result
|| array_sum(array_column($result[$lastKey], 'price')) == $price_array[$lastKey]
) {
unset($ref);
$result[] = &$ref;
}
$ref[] = $row;
}
var_export($result);
preferences:
26.08 ms | 407 KiB | 5 Q