3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = ["categories" => [ [ 'id' => 368, 'name' => 'Aksesuar', 'parentId' => NULL, 'subCategories' => [ [ 'id' => 387, 'name' => 'Saat', 'parentId' => 368, 'subCategories' => [] ], [ 'id' => 394, 'name' => 'Şapka', 'parentId' => 368, 'subCategories' => [] ], [ 'id' => 396, 'name' => 'Takı & Mücevher', 'parentId' => 368, 'subCategories' => [ [ 'id' => 397, 'name' => 'Bileklik', 'parentId' => 396, 'subCategories' => [ [ 'id' => 1238, 'name' => 'Altın Bileklik', 'parentId' => 397, 'subCategories' => [] ] ] ] ] ], ] ] ]]; function recursive(string $needle, array $categories, $id): ?array { foreach ($categories as $cat) { if (array_key_exists($needle, $cat) && $cat[$needle] === $id) { // make sure that if correct category is found, // no subcategories will be included unset($cat['subCategories']); return $cat; } else { $subCat = recursive($needle, $cat['subCategories'], $id); // if the $subCat is not null, it indicates the category // with the correct id, or one of it's parent has been found if ($subCat !== null) { $cat['subCategories'] = $subCat; return $cat; } } } return null; } var_dump(recursive('id', $arr['categories'], 397));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8hvY9
function name:  (null)
number of ops:  11
compiled vars:  !0 = $arr
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   64     1        INIT_FCALL                                                   'var_dump'
          2        INIT_FCALL                                                   'recursive'
          3        SEND_VAL                                                     'id'
          4        FETCH_DIM_R                                          ~2      !0, 'categories'
          5        SEND_VAL                                                     ~2
          6        SEND_VAL                                                     397
          7        DO_FCALL                                          0  $3      
          8        SEND_VAR                                                     $3
          9        DO_ICALL                                                     
         10      > RETURN                                                       1

Function recursive:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 32
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 32
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 31
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/8hvY9
function name:  recursive
number of ops:  36
compiled vars:  !0 = $needle, !1 = $categories, !2 = $id, !3 = $cat, !4 = $subCat
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   43     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
   45     3      > FE_RESET_R                                           $5      !1, ->32
          4    > > FE_FETCH_R                                                   $5, !3, ->32
   46     5    >   ARRAY_KEY_EXISTS                                     ~6      !0, !3
          6      > JMPZ_EX                                              ~6      ~6, ->10
          7    >   FETCH_DIM_R                                          ~7      !3, !0
          8        IS_IDENTICAL                                         ~8      !2, ~7
          9        BOOL                                                 ~6      ~8
         10    > > JMPZ                                                         ~6, ->16
   49    11    >   UNSET_DIM                                                    !3, 'subCategories'
   50    12        VERIFY_RETURN_TYPE                                           !3
         13        FE_FREE                                                      $5
         14      > RETURN                                                       !3
   46    15*       JMP                                                          ->31
   52    16    >   INIT_FCALL_BY_NAME                                           'recursive'
         17        SEND_VAR_EX                                                  !0
         18        CHECK_FUNC_ARG                                               
         19        FETCH_DIM_FUNC_ARG                                   $9      !3, 'subCategories'
         20        SEND_FUNC_ARG                                                $9
         21        SEND_VAR_EX                                                  !2
         22        DO_FCALL                                          0  $10     
         23        ASSIGN                                                       !4, $10
   55    24        TYPE_CHECK                                      1020          !4
         25      > JMPZ                                                         ~12, ->31
   56    26    >   ASSIGN_DIM                                                   !3, 'subCategories'
         27        OP_DATA                                                      !4
   57    28        VERIFY_RETURN_TYPE                                           !3
         29        FE_FREE                                                      $5
         30      > RETURN                                                       !3
   45    31    > > JMP                                                          ->4
         32    >   FE_FREE                                                      $5
   61    33      > RETURN                                                       null
   62    34*       VERIFY_RETURN_TYPE                                           
         35*     > RETURN                                                       null

End of function recursive

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
168.72 ms | 2114 KiB | 15 Q