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);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 37
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 35
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 35
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 26
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jSdsA
function name:  (null)
number of ops:  51
compiled vars:  !0 = $arr, !1 = $sum, !2 = $index, !3 = $result, !4 = $val, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    6     1        INIT_FCALL                                               'array_sum'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $7      
          4        IS_SMALLER_OR_EQUAL                                      250, $7
          5      > JMPZ                                                     ~8, ->37
    8     6    >   ASSIGN                                                   !1, 0
    9     7        ASSIGN                                                   !2, 0
   10     8        ASSIGN_DIM                                               !3, !2
          9        OP_DATA                                                  <array>
   12    10      > FE_RESET_R                                       $12     !0, ->35
         11    > > FE_FETCH_R                                       ~13     $12, !4, ->35
         12    >   ASSIGN                                                   !5, ~13
   13    13        ADD                                              ~15     !1, !4
         14        IS_SMALLER                                               ~15, 250
         15      > JMPZ                                                     ~16, ->26
   15    16    >   FETCH_DIM_W                                      $17     !3, !2
         17        FETCH_DIM_W                                      $18     $17, 'values'
         18        ASSIGN_DIM                                               $18
         19        OP_DATA                                                  !4
   16    20        FETCH_DIM_W                                      $20     !3, !2
         21        FETCH_DIM_W                                      $21     $20, 'elements'
         22        ASSIGN_DIM                                               $21
         23        OP_DATA                                                  !5
   17    24        ASSIGN_OP                                     1          !1, !4
   13    25      > JMP                                                      ->34
   20    26    >   ASSIGN                                                   !1, !4
   21    27        PRE_INC                                                  !2
   22    28        INIT_ARRAY                                       ~27     !4
         29        INIT_ARRAY                                       ~28     ~27, 'values'
         30        INIT_ARRAY                                       ~29     !5
         31        ADD_ARRAY_ELEMENT                                ~28     ~29, 'elements'
         32        ASSIGN_DIM                                               !3, !2
         33        OP_DATA                                                  ~28
   12    34    > > JMP                                                      ->11
         35    >   FE_FREE                                                  $12
    6    36      > JMP                                                      ->47
   27    37    >   INIT_FCALL                                               'array_values'
         38        SEND_VAR                                                 !0
         39        DO_ICALL                                         $30     
         40        INIT_ARRAY                                       ~31     $30, 'values'
         41        INIT_FCALL                                               'array_keys'
         42        SEND_VAR                                                 !0
         43        DO_ICALL                                         $32     
         44        ADD_ARRAY_ELEMENT                                ~31     $32, 'elements'
         45        INIT_ARRAY                                       ~33     ~31
         46        ASSIGN                                                   !3, ~33
   30    47    >   INIT_FCALL                                               'var_dump'
         48        SEND_VAR                                                 !3
         49        DO_ICALL                                                 
         50      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.2 ms | 1008 KiB | 17 Q