3v4l.org

run code in 300+ PHP versions simultaneously
<?php $max = 111; //265; $items = [50, 45, 30, 60, 70, 80]; function nextFitDecreasing(array $items, int $max): array { rsort($items); $result = []; foreach ($items as $item) { if (!isset($pallet) || (array_sum($pallet) + $item) > $max) { unset($pallet); $result[] = &$pallet; } $pallet[] = $item; } return $result; } function firstFitDecreasing(array $items, int $max): array { rsort($items); $result = []; foreach ($items as $item) { foreach ($result as &$pallet) { if (array_sum($pallet) + $item <= $max) { $pallet[] = $item; continue 2; } } $result[] = [$item]; } return $result; } var_export(nextFitDecreasing($items, $max)); echo "\n---\n"; var_export(firstFitDecreasing($items, $max));
Output for git.master, git.master_jit, rfc.property-hooks
array ( 0 => array ( 0 => 80, ), 1 => array ( 0 => 70, ), 2 => array ( 0 => 60, 1 => 50, ), 3 => array ( 0 => 45, 1 => 30, ), ) --- array ( 0 => array ( 0 => 80, 1 => 30, ), 1 => array ( 0 => 70, ), 2 => array ( 0 => 60, 1 => 50, ), 3 => array ( 0 => 45, ), )

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:
75.77 ms | 406 KiB | 5 Q