3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = array ( 0 => array ( 'id' => '1', 'p_pag' => '0', 'link' => 'Contact', 'children' => array ( 0 => array ( 'id' => '2', 'p_pag' => '1', 'link' => 'Contact-County', 'children' => array ( 0 => array ( 'id' => '3', 'p_pag' => '2', 'link' => 'Contact-Town-1', 'children' => array ( ), ), ), ), ), ), ); function getLinkPathById(array $array, string $toId): ?string { foreach ($array as $row) { if ( $row['id'] === $toId || ($deeper = getLinkPathById($row['children'], $toId)) ) { return '/' . $row['link'] . ($deeper ?? null); } } return null; } echo "1: " . getLinkPathById($array, 1) . PHP_EOL; echo "2: " . getLinkPathById($array, 2) . PHP_EOL; echo "3: " . getLinkPathById($array, 3) . PHP_EOL; echo "4: " . getLinkPathById($array, 4);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AMInf
function name:  (null)
number of ops:  29
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   46     1        INIT_FCALL                                                   'getlinkpathbyid'
          2        SEND_VAR                                                     !0
          3        SEND_VAL                                                     1
          4        DO_FCALL                                          0  $2      
          5        CONCAT                                               ~3      '1%3A+', $2
          6        CONCAT                                               ~4      ~3, '%0A'
          7        ECHO                                                         ~4
   47     8        INIT_FCALL                                                   'getlinkpathbyid'
          9        SEND_VAR                                                     !0
         10        SEND_VAL                                                     2
         11        DO_FCALL                                          0  $5      
         12        CONCAT                                               ~6      '2%3A+', $5
         13        CONCAT                                               ~7      ~6, '%0A'
         14        ECHO                                                         ~7
   48    15        INIT_FCALL                                                   'getlinkpathbyid'
         16        SEND_VAR                                                     !0
         17        SEND_VAL                                                     3
         18        DO_FCALL                                          0  $8      
         19        CONCAT                                               ~9      '3%3A+', $8
         20        CONCAT                                               ~10     ~9, '%0A'
         21        ECHO                                                         ~10
   49    22        INIT_FCALL                                                   'getlinkpathbyid'
         23        SEND_VAR                                                     !0
         24        SEND_VAL                                                     4
         25        DO_FCALL                                          0  $11     
         26        CONCAT                                               ~12     '4%3A+', $11
         27        ECHO                                                         ~12
         28      > RETURN                                                       1

Function getlinkpathbyid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 25
Branch analysis from position: 4
2 jumps found. (Code = 47) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 24
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/AMInf
function name:  getLinkPathById
number of ops:  29
compiled vars:  !0 = $array, !1 = $toId, !2 = $row, !3 = $deeper
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   33     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   35     2      > FE_RESET_R                                           $4      !0, ->25
          3    > > FE_FETCH_R                                                   $4, !2, ->25
   37     4    >   FETCH_DIM_R                                          ~5      !2, 'id'
          5        IS_IDENTICAL                                         ~6      !1, ~5
          6      > JMPNZ_EX                                             ~6      ~6, ->15
   38     7    >   INIT_FCALL_BY_NAME                                           'getLinkPathById'
          8        CHECK_FUNC_ARG                                               
          9        FETCH_DIM_FUNC_ARG                                   $7      !2, 'children'
         10        SEND_FUNC_ARG                                                $7
         11        SEND_VAR_EX                                                  !1
         12        DO_FCALL                                          0  $8      
         13        ASSIGN                                               ~9      !3, $8
         14        BOOL                                                 ~6      ~9
         15    > > JMPZ                                                         ~6, ->24
   40    16    >   FETCH_DIM_R                                          ~10     !2, 'link'
         17        CONCAT                                               ~11     '%2F', ~10
         18        COALESCE                                             ~12     !3
         19        QM_ASSIGN                                            ~12     null
         20        CONCAT                                               ~13     ~11, ~12
         21        VERIFY_RETURN_TYPE                                           ~13
         22        FE_FREE                                                      $4
         23      > RETURN                                                       ~13
   35    24    > > JMP                                                          ->3
         25    >   FE_FREE                                                      $4
   43    26      > RETURN                                                       null
   44    27*       VERIFY_RETURN_TYPE                                           
         28*     > RETURN                                                       null

End of function getlinkpathbyid

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
169.83 ms | 1894 KiB | 17 Q