3v4l.org

run code in 300+ PHP versions simultaneously
<?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 ]; $bucket_sum = 0; $bucket_index = 0; $result = []; foreach ($line_items as $row) { $result[$bucket_index][] = $row; $bucket_sum += $row['price']; if ($bucket_sum >= $price_array[$bucket_index]) { $bucket_sum = 0; ++$bucket_index; } } var_export($result);

preferences:
24.49 ms | 404 KiB | 5 Q