To create an array where the sum of each entry would not exceed a given amount, you can use an iterative approach.
Let's start with an empty array and a variable representing the working index of that array. As we go through the input array, we add the maximum possible remaining quantity to the new array. If we reach the limit, we increment the index variable. And we continue as long as the input array has not been completely browsed.
Code:
const MAX_SUM = 50;
$total = []; // Store the new data
$curKey = 0; // Store the current key of $total array.
foreach ($array as $key => $value) {
while ($value) {
// Get the current sum for the current key:
$curSum = array_sum($total[$curKey] ?? []);
// If the max was reached, we can go to the next key:
if ($curSum == MAX_SUM) $curKey++;
// Now, compute if the value to add (max to reach 50);
$add = $value + $curSum > MAX_SUM // If above,
? MAX_SUM - $curSum // got only the difference,
: $value; // else, use the full value.
// Add the value
$total[$curKey][$key] = $add;
// Finally, remove what we added just before.
$value -= $add;
}
}
print_r($total);
Outputs :
Array (
[0] => Array (
[5] => 50
)
[1] => Array (
[5] => 50
)
[2] => Array (
[5] => 42
[2] => 8
)
[3] => Array (
[2] => 49
[18] => 1
)
[4] => Array (
[18] => 36
)
)
See also a the nice answer of @mickmackusa.