3v4l.org

run code in 300+ PHP versions simultaneously
<?php $items = [ [ 'qty' => 1, 'weight' => 1, ], [ 'qty' => 2, 'weight' => 1, ], [ 'qty' => 3, 'weight' => 1, ], [ 'qty' => 1, 'weight' => 5, ], ]; $weights = array_column($items, 'weight'); $result =0; Foreach(array_unique($weights) as $weight){ $weightitems = preg_grep("/". $weight . "/", $weights); $result += array_sum(array_column(array_intersect_key($items, $weightitems), "qty")) * $weight; } Echo $result;
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 33
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 33
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
filename:       /in/V293P
function name:  (null)
number of ops:  36
compiled vars:  !0 = $items, !1 = $weights, !2 = $result, !3 = $weight, !4 = $weightitems
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
   23     1        INIT_FCALL                                               'array_column'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'weight'
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !1, $6
   24     6        ASSIGN                                                   !2, 0
   25     7        INIT_FCALL                                               'array_unique'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $9      
         10      > FE_RESET_R                                       $10     $9, ->33
         11    > > FE_FETCH_R                                               $10, !3, ->33
   26    12    >   INIT_FCALL                                               'preg_grep'
         13        CONCAT                                           ~11     '%2F', !3
         14        CONCAT                                           ~12     ~11, '%2F'
         15        SEND_VAL                                                 ~12
         16        SEND_VAR                                                 !1
         17        DO_ICALL                                         $13     
         18        ASSIGN                                                   !4, $13
   27    19        INIT_FCALL                                               'array_sum'
         20        INIT_FCALL                                               'array_column'
         21        INIT_FCALL                                               'array_intersect_key'
         22        SEND_VAR                                                 !0
         23        SEND_VAR                                                 !4
         24        DO_ICALL                                         $15     
         25        SEND_VAR                                                 $15
         26        SEND_VAL                                                 'qty'
         27        DO_ICALL                                         $16     
         28        SEND_VAR                                                 $16
         29        DO_ICALL                                         $17     
         30        MUL                                              ~18     !3, $17
         31        ASSIGN_OP                                     1          !2, ~18
   25    32      > JMP                                                      ->11
         33    >   FE_FREE                                                  $10
   29    34        ECHO                                                     !2
   30    35      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.12 ms | 1401 KiB | 23 Q