3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ '5' => 142, '2' => 57, '0' => 0, '18' => 37, '3' => 13, '6' => 101, ]; $groupLimit = 50; $result = []; $groupKey = 0; foreach ($array as $key => $value) { while ($value) { $sum = array_sum($result[$groupKey] ?? []); // get group sum $value -= $result[$groupKey][$key] = min($groupLimit - $sum, $value); // push key with limited value; decrease value $groupKey += ($sum + $result[$groupKey][$key] === $groupLimit); // only increment group key if at $groupLimit } } var_export($result);
Output for git.master_jit, git.master, rfc.property-hooks
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, 3 => 13, 6 => 1, ), 5 => array ( 6 => 50, ), 6 => array ( 6 => 50, ), )

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:
39.66 ms | 402 KiB | 8 Q