3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = Array( 42 => 10, 55 => 20, 56 => 10, 57 => 4, 58 => 5, 59 => 240, 60 => 123, ); // Can we do all in one subarray? if(array_sum($arr) >= 250){ //No $sum = 0; $index = 0; $result[$index] =[]; //Loop the array and keep the sum in memory foreach($arr as $key => $val){ if($sum + $val < 250){ //Less than 250, just keep adding the values $result[$index]['values'][] = $val; $result[$index]['elements'][] = $key; $sum += $val; }else{ // More than 250, create a new subarray and reset sum $sum = $val; $index++; $result[$index] = ['values' => [$val], 'elements' => [$key]]; } } }else{ //Yes, group the values and keys $result = [['values' => array_values($arr), 'elements' => array_keys($arr)]]; } var_dump($result);
Output for git.master, git.master_jit, rfc.property-hooks
array(3) { [0]=> array(2) { ["values"]=> array(5) { [0]=> int(10) [1]=> int(20) [2]=> int(10) [3]=> int(4) [4]=> int(5) } ["elements"]=> array(5) { [0]=> int(42) [1]=> int(55) [2]=> int(56) [3]=> int(57) [4]=> int(58) } } [1]=> array(2) { ["values"]=> array(1) { [0]=> int(240) } ["elements"]=> array(1) { [0]=> int(59) } } [2]=> array(2) { ["values"]=> array(1) { [0]=> int(123) } ["elements"]=> array(1) { [0]=> int(60) } } }

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:
112.65 ms | 407 KiB | 5 Q