3v4l.org

run code in 300+ PHP versions simultaneously
<?php $max_weight = 100; // init with some random weights $objects = []; for ($i = 1; $i <= 100; $i++) { $objects[] = (object) ['id' => $i, 'weight' => rand(1, 50)]; } // sort by weight, heaviest first usort($objects, function ($a, $b) { return $b->weight <=> $a->weight; }); $containers = []; $container_number = 0; $total_weight = 0; while (true) { $container_number++; $container = []; $container_weight = 0; foreach ($objects as $key => $object) { // add this object to the container if it won't push it over the max weight if ($container_weight + $object->weight <= $max_weight) { $container[] = $object; unset($objects[$key]); $container_weight += $object->weight; $total_weight += $object->weight; if ($container_weight == $max_weight) { break; } } } $containers[$container_number] = $container; $container_contents = count($container); echo 'Container #' . $container_number . ' total weight: ' . $container_weight . ' (' . $container_contents . ' items)' . PHP_EOL; if (empty($objects)) { break; } } echo 'Total weight: ' . $total_weight . PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 4
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 63, Position 2 = 25
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 77) Position 1 = 29, Position 2 = 46
Branch analysis from position: 29
2 jumps found. (Code = 78) Position 1 = 30, Position 2 = 46
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 45
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 45
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 62
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
Branch analysis from position: 62
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
Branch analysis from position: 45
Branch analysis from position: 46
Branch analysis from position: 46
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 4
Branch analysis from position: 16
Branch analysis from position: 4
filename:       /in/KWrZo
function name:  (null)
number of ops:  67
compiled vars:  !0 = $max_weight, !1 = $objects, !2 = $i, !3 = $containers, !4 = $container_number, !5 = $total_weight, !6 = $container, !7 = $container_weight, !8 = $object, !9 = $key, !10 = $container_contents
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, 100
    6     1        ASSIGN                                                   !1, <array>
    7     2        ASSIGN                                                   !2, 1
          3      > JMP                                                      ->14
    8     4    >   INIT_ARRAY                                       ~15     !2, 'id'
          5        INIT_FCALL                                               'rand'
          6        SEND_VAL                                                 1
          7        SEND_VAL                                                 50
          8        DO_ICALL                                         $16     
          9        ADD_ARRAY_ELEMENT                                ~15     $16, 'weight'
         10        CAST                                          8  ~17     ~15
         11        ASSIGN_DIM                                               !1
         12        OP_DATA                                                  ~17
    7    13        PRE_INC                                                  !2
         14    >   IS_SMALLER_OR_EQUAL                                      !2, 100
         15      > JMPNZ                                                    ~19, ->4
   12    16    >   INIT_FCALL                                               'usort'
         17        SEND_REF                                                 !1
         18        DECLARE_LAMBDA_FUNCTION                          ~20     [0]
   14    19        SEND_VAL                                                 ~20
   12    20        DO_ICALL                                                 
   16    21        ASSIGN                                                   !3, <array>
   17    22        ASSIGN                                                   !4, 0
   18    23        ASSIGN                                                   !5, 0
   20    24      > JMP                                                      ->62
   22    25    >   PRE_INC                                                  !4
   24    26        ASSIGN                                                   !6, <array>
   25    27        ASSIGN                                                   !7, 0
   27    28      > FE_RESET_R                                       $28     !1, ->46
         29    > > FE_FETCH_R                                       ~29     $28, !8, ->46
         30    >   ASSIGN                                                   !9, ~29
   29    31        FETCH_OBJ_R                                      ~31     !8, 'weight'
         32        ADD                                              ~32     !7, ~31
         33        IS_SMALLER_OR_EQUAL                                      ~32, !0
         34      > JMPZ                                                     ~33, ->45
   30    35    >   ASSIGN_DIM                                               !6
         36        OP_DATA                                                  !8
   31    37        UNSET_DIM                                                !1, !9
   32    38        FETCH_OBJ_R                                      ~35     !8, 'weight'
         39        ASSIGN_OP                                     1          !7, ~35
   33    40        FETCH_OBJ_R                                      ~37     !8, 'weight'
         41        ASSIGN_OP                                     1          !5, ~37
   34    42        IS_EQUAL                                                 !7, !0
         43      > JMPZ                                                     ~39, ->45
   35    44    > > JMP                                                      ->46
   27    45    > > JMP                                                      ->29
         46    >   FE_FREE                                                  $28
   40    47        ASSIGN_DIM                                               !3, !4
         48        OP_DATA                                                  !6
   41    49        COUNT                                            ~41     !6
         50        ASSIGN                                                   !10, ~41
   43    51        CONCAT                                           ~43     'Container+%23', !4
         52        CONCAT                                           ~44     ~43, '+total+weight%3A+'
         53        CONCAT                                           ~45     ~44, !7
         54        CONCAT                                           ~46     ~45, '+%28'
         55        CONCAT                                           ~47     ~46, !10
         56        CONCAT                                           ~48     ~47, '+items%29'
         57        CONCAT                                           ~49     ~48, '%0A'
         58        ECHO                                                     ~49
   45    59        ISSET_ISEMPTY_CV                                         !1
         60      > JMPZ                                                     ~50, ->62
   46    61    > > JMP                                                      ->63
   20    62    > > JMPNZ                                                    <true>, ->25
   50    63    >   CONCAT                                           ~51     'Total+weight%3A+', !5
         64        CONCAT                                           ~52     ~51, '%0A'
         65        ECHO                                                     ~52
   51    66      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KWrZo
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   13     2        FETCH_OBJ_R                                      ~2      !1, 'weight'
          3        FETCH_OBJ_R                                      ~3      !0, 'weight'
          4        SPACESHIP                                        ~4      ~2, ~3
          5      > RETURN                                                   ~4
   14     6*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.69 ms | 1020 KiB | 15 Q