3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1 = [ [ 'name' => 'internal_evidence', 'price' => 30, 'course_id' => 3 ], [ 'name' => 'international_evidence', 'price' => 450, 'course_id' => 3 ], [ 'name' => 'internal_evidence', 'price' => 10, 'course_id' => 1 ], [ 'name' => 'technical_evidence', 'price' => 134, 'course_id' => 3 ], ]; $array2 = [ [ 'id' => 3, 'name' => 'graphic', 'price' => 150, 'attr' => [ 'internal_evidence' => 'true', 'international_evidence' => 'false', 'technical_evidence' => 'true' ] ], [ 'id' => 5, 'name' => '3dmax', 'price' => 300, 'attr' => [ ] ], [ 'id' => 1, 'name' => 'ICDL', 'price' => 480, 'attr' => [ 'internal_evidence' => 'true', ] ], ]; $array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) { $byIdAndName[$entry['course_id']][$entry['name']] = $entry; return $byIdAndName; }); $array2 = array_map(static function ($entry) use ($array1ByIdAndName) { $entry['total_price'] = $entry['price']; $entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) { if ($entry['attr'][$attrName] === 'true') { $attrs[] = $attrName; $entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price']; } return $attrs; }, []); return $entry; }, $array2); ?> <?php foreach ($array2 as $entry): ?> <h2><?= $entry['name'] ?></h2> <?php foreach ($entry['attr'] as $attrName): ?> <p><?= $attrName ?></p> <?php endforeach ?> <small>course price : <?= $entry['price'] ?></small> <?php if ($entry['total_price'] > 0): ?> <small><?= $entry['total_price'] ?></small> <?php endif ?> <?php endforeach ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 43
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 43
Branch analysis from position: 18
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 29
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 41
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 41
Branch analysis from position: 29
Branch analysis from position: 43
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
filename:       /in/nS3Gl
function name:  (null)
number of ops:  45
compiled vars:  !0 = $array1, !1 = $array2, !2 = $array1ByIdAndName, !3 = $entry, !4 = $attrName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    1     0  E >   ASSIGN                                                   !0, <array>
   30     1        ASSIGN                                                   !1, <array>
   61     2        INIT_FCALL                                               'array_reduce'
          3        SEND_VAR                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
   64     5        SEND_VAL                                                 ~7
   61     6        DO_ICALL                                         $8      
          7        ASSIGN                                                   !2, $8
   66     8        INIT_FCALL                                               'array_map'
          9        DECLARE_LAMBDA_FUNCTION                          ~10     [1]
         10        BIND_LEXICAL                                             ~10, !2
   76    11        SEND_VAL                                                 ~10
         12        SEND_VAR                                                 !1
   66    13        DO_ICALL                                         $11     
         14        ASSIGN                                                   !1, $11
   79    15        ECHO                                                     '%0A'
   80    16      > FE_RESET_R                                       $13     !1, ->43
         17    > > FE_FETCH_R                                               $13, !3, ->43
   81    18    >   ECHO                                                     '%0A%3Ch2%3E'
   82    19        FETCH_DIM_R                                      ~14     !3, 'name'
         20        ECHO                                                     ~14
         21        ECHO                                                     '%3C%2Fh2%3E%0A'
   83    22        FETCH_DIM_R                                      ~15     !3, 'attr'
         23      > FE_RESET_R                                       $16     ~15, ->29
         24    > > FE_FETCH_R                                               $16, !4, ->29
   84    25    >   ECHO                                                     '%3Cp%3E'
         26        ECHO                                                     !4
         27        ECHO                                                     '%3C%2Fp%3E%0A'
   83    28      > JMP                                                      ->24
         29    >   FE_FREE                                                  $16
   86    30        ECHO                                                     '%3Csmall%3Ecourse+price+%3A+'
         31        FETCH_DIM_R                                      ~17     !3, 'price'
         32        ECHO                                                     ~17
         33        ECHO                                                     '%3C%2Fsmall%3E%0A'
   87    34        FETCH_DIM_R                                      ~18     !3, 'total_price'
         35        IS_SMALLER                                               0, ~18
         36      > JMPZ                                                     ~19, ->41
   88    37    >   ECHO                                                     '%3Csmall%3E'
         38        FETCH_DIM_R                                      ~20     !3, 'total_price'
         39        ECHO                                                     ~20
         40        ECHO                                                     '%3C%2Fsmall%3E%0A'
   90    41    >   ECHO                                                     '%0A'
   80    42      > JMP                                                      ->17
         43    >   FE_FREE                                                  $13
   91    44      > 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/nS3Gl
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $byIdAndName, !1 = $entry
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   62     2        FETCH_DIM_R                                      ~2      !1, 'course_id'
          3        FETCH_DIM_R                                      ~4      !1, 'name'
          4        FETCH_DIM_W                                      $3      !0, ~2
          5        ASSIGN_DIM                                               $3, ~4
          6        OP_DATA                                                  !1
   63     7      > RETURN                                                   !0
   64     8*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nS3Gl
function name:  {closure}
number of ops:  21
compiled vars:  !0 = $entry, !1 = $array1ByIdAndName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   67     2        FETCH_DIM_R                                      ~3      !0, 'price'
          3        ASSIGN_DIM                                               !0, 'total_price'
          4        OP_DATA                                                  ~3
   68     5        INIT_FCALL                                               'array_reduce'
          6        INIT_FCALL                                               'array_keys'
          7        FETCH_DIM_R                                      ~5      !0, 'attr'
          8        SEND_VAL                                                 ~5
          9        DO_ICALL                                         $6      
         10        SEND_VAR                                                 $6
         11        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
         12        BIND_LEXICAL                                             ~7, !1
         13        BIND_LEXICAL                                             ~7, !0
   74    14        SEND_VAL                                                 ~7
         15        SEND_VAL                                                 <array>
   68    16        DO_ICALL                                         $8      
         17        ASSIGN_DIM                                               !0, 'attr'
   74    18        OP_DATA                                                  $8
   75    19      > RETURN                                                   !0
   76    20*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/nS3Gl
function name:  {closure}
number of ops:  18
compiled vars:  !0 = $attrs, !1 = $attrName, !2 = $array1ByIdAndName, !3 = $entry
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   69     4        FETCH_DIM_R                                      ~4      !3, 'attr'
          5        FETCH_DIM_R                                      ~5      ~4, !1
          6        IS_IDENTICAL                                             ~5, 'true'
          7      > JMPZ                                                     ~6, ->16
   70     8    >   ASSIGN_DIM                                               !0
          9        OP_DATA                                                  !1
   71    10        FETCH_DIM_R                                      ~9      !3, 'id'
         11        FETCH_DIM_R                                      ~10     !2, ~9
         12        FETCH_DIM_R                                      ~11     ~10, !1
         13        FETCH_DIM_R                                      ~12     ~11, 'price'
         14        ASSIGN_DIM_OP                +=               1          !3, 'total_price'
         15        OP_DATA                                                  ~12
   73    16    > > RETURN                                                   !0
   74    17*     > RETURN                                                   null

End of Dynamic Function 0

End of Dynamic Function 1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.07 ms | 1009 KiB | 16 Q