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);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 31
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 31
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 8
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 8
Branch analysis from position: 30
Branch analysis from position: 8
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/s8GMC
function name:  (null)
number of ops:  36
compiled vars:  !0 = $array, !1 = $groupLimit, !2 = $result, !3 = $groupKey, !4 = $value, !5 = $key, !6 = $sum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   12     1        ASSIGN                                                   !1, 50
   14     2        ASSIGN                                                   !2, <array>
   15     3        ASSIGN                                                   !3, 0
   16     4      > FE_RESET_R                                       $11     !0, ->31
          5    > > FE_FETCH_R                                       ~12     $11, !4, ->31
          6    >   ASSIGN                                                   !5, ~12
   17     7      > JMP                                                      ->29
   18     8    >   INIT_FCALL                                               'array_sum'
          9        FETCH_DIM_IS                                     ~14     !2, !3
         10        COALESCE                                         ~15     ~14
         11        QM_ASSIGN                                        ~15     <array>
         12        SEND_VAL                                                 ~15
         13        DO_ICALL                                         $16     
         14        ASSIGN                                                   !6, $16
   19    15        INIT_FCALL                                               'min'
         16        SUB                                              ~20     !1, !6
         17        SEND_VAL                                                 ~20
         18        SEND_VAR                                                 !4
         19        DO_ICALL                                         $21     
         20        FETCH_DIM_W                                      $18     !2, !3
         21        ASSIGN_DIM                                       ~19     $18, !5
         22        OP_DATA                                                  $21
         23        ASSIGN_OP                                     2          !4, ~19
   20    24        FETCH_DIM_R                                      ~23     !2, !3
         25        FETCH_DIM_R                                      ~24     ~23, !5
         26        ADD                                              ~25     !6, ~24
         27        IS_IDENTICAL                                     ~26     !1, ~25
         28        ASSIGN_OP                                     1          !3, ~26
   17    29    > > JMPNZ                                                    !4, ->8
   16    30    > > JMP                                                      ->5
         31    >   FE_FREE                                                  $11
   23    32        INIT_FCALL                                               'var_export'
         33        SEND_VAR                                                 !2
         34        DO_ICALL                                                 
         35      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.87 ms | 1404 KiB | 19 Q