3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 0 => 50, 1 => 200, 2 => 75, 3 => 150, 4 => 100, 5 => 125, 6 => 150, 7 => 300, 8 => 300, 9 => 300, 10 => 300, 11 => 300, 12 => 300, 13 => 300, 14 => 300, 15 => 300, ]; function consolidate(array $array, int $capacity = 500): array { arsort($array); $bins = [[]]; foreach ($array as $value) { $added = false; foreach ($bins as &$bin) { if ($capacity >= (array_sum($bin) + $value)) { $bin[] = $value; $added = true; continue 2; } } if (!$added) { $bins[] = [$value]; } } return array_map('array_sum', array_filter($bins)); } var_export(consolidate($array));
Output for git.master, git.master_jit, rfc.property-hooks
array ( 0 => 500, 1 => 500, 2 => 450, 3 => 500, 4 => 400, 5 => 300, 6 => 300, 7 => 300, 8 => 300, )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
30.85 ms | 401 KiB | 8 Q