3v4l.org

run code in 300+ PHP versions simultaneously
<?php $products = array ( 0 => array ( 'product_id' => 2, 'customer_id' => 1820, 'order_id' => 'M-AAH-959', 'quantity' => 5, 'weight' => 1.3, 'cubic' => 0.00267, 'total_weight' => 6.5, 'total_cubic' => 0.00267 ), 1 => array ( 'product_id' => 3, 'customer_id' => 1820, 'order_id' => 'M-AAH-959', 'quantity' => 6, 'weight' => 1.5, 'cubic' => 0.00348, 'total_weight' => 9, 'total_cubic' => 0.00348 ), 2 => array ( 'product_id' => 8, 'customer_id' => 1820, 'order_id' => 'M-AAH-959', 'quantity' => 7, 'weight' => 1.5, 'cubic' => 0.00267, 'total_weight' => 10.5, 'total_cubic' => 0.00267 ) ); function pack_boxes($products, $box_weight) { $boxes = array(); $this_box_weight = 0; $box_number = 0; foreach ($products as $product) { // will all this product fit? if ($product['total_weight'] < $box_weight - $this_box_weight) { // yes - add this product to the box $boxes[$box_number][] = $product; $this_box_weight += $product['total_weight']; $boxes[$box_number]['box_weight'] = $this_box_weight; } else { // no - add the part that will fit to this box $num_products = floor(($box_weight - $this_box_weight) / $product['weight']); $balance = $product['quantity'] - $num_products; $product['quantity'] = $num_products; $product['total_weight'] = $num_products * $product['weight']; $boxes[$box_number][] = $product; $boxes[$box_number]['box_weight'] = $this_box_weight + $num_products * $product['weight']; // add the balance to a new box $box_number++; $product['quantity'] = $balance; $this_box_weight = $product['total_weight'] = $balance * $product['weight']; $boxes[$box_number][] = $product; $boxes[$box_number]['box_weight'] = $this_box_weight; } } return $boxes; } $boxes = pack_boxes($products, 12); print_r(array_column($boxes, 'box_weight')); print_r($boxes);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/uNorh
function name:  (null)
number of ops:  17
compiled vars:  !0 = $products, !1 = $boxes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   70     1        INIT_FCALL                                               'pack_boxes'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 12
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !1, $3
   71     6        INIT_FCALL                                               'print_r'
          7        INIT_FCALL                                               'array_column'
          8        SEND_VAR                                                 !1
          9        SEND_VAL                                                 'box_weight'
         10        DO_ICALL                                         $5      
         11        SEND_VAR                                                 $5
         12        DO_ICALL                                                 
   72    13        INIT_FCALL                                               'print_r'
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                                 
         16      > RETURN                                                   1

Function pack_boxes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 60
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 60
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 20
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
filename:       /in/uNorh
function name:  pack_boxes
number of ops:  63
compiled vars:  !0 = $products, !1 = $box_weight, !2 = $boxes, !3 = $this_box_weight, !4 = $box_number, !5 = $product, !6 = $num_products, !7 = $balance
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   40     2        ASSIGN                                                   !2, <array>
   41     3        ASSIGN                                                   !3, 0
   42     4        ASSIGN                                                   !4, 0
   43     5      > FE_RESET_R                                       $11     !0, ->60
          6    > > FE_FETCH_R                                               $11, !5, ->60
   45     7    >   FETCH_DIM_R                                      ~12     !5, 'total_weight'
          8        SUB                                              ~13     !1, !3
          9        IS_SMALLER                                               ~12, ~13
         10      > JMPZ                                                     ~14, ->20
   47    11    >   FETCH_DIM_W                                      $15     !2, !4
         12        ASSIGN_DIM                                               $15
         13        OP_DATA                                                  !5
   48    14        FETCH_DIM_R                                      ~17     !5, 'total_weight'
         15        ASSIGN_OP                                     1          !3, ~17
   49    16        FETCH_DIM_W                                      $19     !2, !4
         17        ASSIGN_DIM                                               $19, 'box_weight'
         18        OP_DATA                                                  !3
   45    19      > JMP                                                      ->59
   53    20    >   INIT_FCALL                                               'floor'
         21        SUB                                              ~21     !1, !3
         22        FETCH_DIM_R                                      ~22     !5, 'weight'
         23        DIV                                              ~23     ~21, ~22
         24        SEND_VAL                                                 ~23
         25        DO_ICALL                                         $24     
         26        ASSIGN                                                   !6, $24
   54    27        FETCH_DIM_R                                      ~26     !5, 'quantity'
         28        SUB                                              ~27     ~26, !6
         29        ASSIGN                                                   !7, ~27
   55    30        ASSIGN_DIM                                               !5, 'quantity'
         31        OP_DATA                                                  !6
   56    32        FETCH_DIM_R                                      ~31     !5, 'weight'
         33        MUL                                              ~32     !6, ~31
         34        ASSIGN_DIM                                               !5, 'total_weight'
         35        OP_DATA                                                  ~32
   57    36        FETCH_DIM_W                                      $33     !2, !4
         37        ASSIGN_DIM                                               $33
         38        OP_DATA                                                  !5
   58    39        FETCH_DIM_R                                      ~37     !5, 'weight'
         40        MUL                                              ~38     !6, ~37
         41        ADD                                              ~39     !3, ~38
         42        FETCH_DIM_W                                      $35     !2, !4
         43        ASSIGN_DIM                                               $35, 'box_weight'
         44        OP_DATA                                                  ~39
   60    45        PRE_INC                                                  !4
   61    46        ASSIGN_DIM                                               !5, 'quantity'
         47        OP_DATA                                                  !7
   62    48        FETCH_DIM_R                                      ~43     !5, 'weight'
         49        MUL                                              ~44     !7, ~43
         50        ASSIGN_DIM                                       ~42     !5, 'total_weight'
         51        OP_DATA                                                  ~44
         52        ASSIGN                                                   !3, ~42
   63    53        FETCH_DIM_W                                      $46     !2, !4
         54        ASSIGN_DIM                                               $46
         55        OP_DATA                                                  !5
   64    56        FETCH_DIM_W                                      $48     !2, !4
         57        ASSIGN_DIM                                               $48, 'box_weight'
         58        OP_DATA                                                  !3
   43    59    > > JMP                                                      ->6
         60    >   FE_FREE                                                  $11
   67    61      > RETURN                                                   !2
   68    62*     > RETURN                                                   null

End of function pack_boxes

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
136.42 ms | 1011 KiB | 17 Q