3v4l.org

run code in 300+ PHP versions simultaneously
<?php //lets call your $BulkArray = $values; $values=array( 1 => 100, 2 => 300, 3 => 100, 4 => 50, 5 => 50, 6 => 50, 7 => 50, 8 => 50, 9 => 500, 10 => 200, 11 => 100 ); $sum=0; // to sum up until 500 $Vals500=array(); // array to store index with <= 500 $i=0; // index for new aaray that will store keys that made up <= 500 $key1=1; //value to iterate the array defined above //print_r($values); echo '<br/>'; for($key=1; $key <= count($values); $key++){ $sum = $sum + $values[$key]; // get sum of values if($sum <= 500){ $Vals500[$i][]=$key; } //append all key of sum ==500 if($sum == 500) { $i++; $key=$key1++; $sum=0; } //check sum then reinitialize values } foreach($Vals500 as $val){} //search for second array with many indeces in $Vals500. NB: i fail this place echo print_r($Vals500); print 'the highest succession found is '; print_r($Vals500[2]); // ?>
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 ) [2] => Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 ) ) 1the highest succession found is Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 )

preferences:
307.45 ms | 407 KiB | 378 Q