<?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
];
var_export(
array_reduce(
$line_items,
function($result, $row) use ($price_array) {
$index = (int) array_key_last($result);
$index += array_sum(array_column($result[$index] ?? [], 'price')) == $price_array[$index];
$result[$index][] = $row;
return $result;
},
[]
)
);
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- array (
0 =>
array (
0 =>
array (
'price' => 41.99,
'id' => 12,
'quantity' => 1,
),
1 =>
array (
'price' => 155.95,
'id' => 11,
'quantity' => 1,
),
),
1 =>
array (
0 =>
array (
'price' => 21,
'id' => 13,
'quantity' => 1,
),
),
)
preferences:
82.7 ms | 407 KiB | 5 Q