3v4l.org

run code in 300+ PHP versions simultaneously
<?php function is_cyclic(array &$array) { $lastKey = array_key_last($array); if ($lastKey === null) { // Array is empty return false; } static $marker; if ($marker === null) { $marker = new stdClass(); } if ($array[$lastKey] === $marker) { return true; } $array[] = $marker; foreach ($array as &$item) { if (is_array($item) && is_cyclic($item)) { array_pop($array); return true; } } array_pop($array); return false; } // Contains reference to itself $v = [1, 2, 3]; $v[1] = &$v; // Contains nested array with reference to $x $x = [1, [2, 3]]; $x[1][1] = &$x; // Contains nested array with reference to an ancestor $y = [1, [2, [3, 4]]]; $y[1][1][1] = &$y[1]; // Nested array contains reference to itself $z = [1, [2, 3]]; $z[1][1] = &$z[1]; // This array is not recursive $p = [1, 2, [3, 4, [5, 6, 7]]]; foreach (['v', 'x', 'y', 'z', 'p'] as $var) { $arr = &${$var}; echo "Array \$$var | Cyclic: "; var_export(is_cyclic($arr)); echo "\n"; echo "----------------\n"; var_dump($arr); echo "\n\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 42
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 42
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 42
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 42
filename:       /in/GG2f0
function name:  (null)
number of ops:  44
compiled vars:  !0 = $v, !1 = $x, !2 = $y, !3 = $z, !4 = $p, !5 = $var, !6 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   ASSIGN                                                   !0, <array>
   30     1        FETCH_DIM_W                                      $8      !0, 1
          2        ASSIGN_REF                                               $8, !0
   33     3        ASSIGN                                                   !1, <array>
   34     4        FETCH_DIM_W                                      $11     !1, 1
          5        FETCH_DIM_W                                      $12     $11, 1
          6        ASSIGN_REF                                               $12, !1
   37     7        ASSIGN                                                   !2, <array>
   38     8        FETCH_DIM_W                                      $18     !2, 1
          9        MAKE_REF                                         $19     $18
         10        FETCH_DIM_W                                      $15     !2, 1
         11        FETCH_DIM_W                                      $16     $15, 1
         12        FETCH_DIM_W                                      $17     $16, 1
         13        ASSIGN_REF                                               $17, $19
   41    14        ASSIGN                                                   !3, <array>
   42    15        FETCH_DIM_W                                      $24     !3, 1
         16        MAKE_REF                                         $25     $24
         17        FETCH_DIM_W                                      $22     !3, 1
         18        FETCH_DIM_W                                      $23     $22, 1
         19        ASSIGN_REF                                               $23, $25
   45    20        ASSIGN                                                   !4, <array>
   48    21      > FE_RESET_R                                       $28     <array>, ->42
         22    > > FE_FETCH_R                                               $28, !5, ->42
   49    23    >   FETCH_W                      local               $29     !5
         24        ASSIGN_REF                                               !6, $29
   50    25        ROPE_INIT                                     3  ~32     'Array+%24'
         26        ROPE_ADD                                      1  ~32     ~32, !5
         27        ROPE_END                                      2  ~31     ~32, '+%7C+Cyclic%3A+'
         28        ECHO                                                     ~31
   51    29        INIT_FCALL                                               'var_export'
         30        INIT_FCALL                                               'is_cyclic'
         31        SEND_REF                                                 !6
         32        DO_FCALL                                      0  $34     
         33        SEND_VAR                                                 $34
         34        DO_ICALL                                                 
   52    35        ECHO                                                     '%0A'
   53    36        ECHO                                                     '----------------%0A'
   54    37        INIT_FCALL                                               'var_dump'
         38        SEND_VAR                                                 !6
         39        DO_ICALL                                                 
   55    40        ECHO                                                     '%0A%0A'
   48    41      > JMP                                                      ->22
         42    >   FE_FREE                                                  $28
   56    43      > RETURN                                                   1

Function is_cyclic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 125) Position 1 = 21, Position 2 = 35
Branch analysis from position: 21
2 jumps found. (Code = 126) Position 1 = 22, Position 2 = 35
Branch analysis from position: 22
2 jumps found. (Code = 46) Position 1 = 24, Position 2 = 28
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 34
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 28
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
Branch analysis from position: 14
filename:       /in/GG2f0
function name:  is_cyclic
number of ops:  41
compiled vars:  !0 = $array, !1 = $lastKey, !2 = $marker, !3 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'array_key_last'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
    6     5        TYPE_CHECK                                    2          !1
          6      > JMPZ                                                     ~6, ->8
    8     7    > > RETURN                                                   <false>
   10     8    >   BIND_STATIC                                              !2
   11     9        TYPE_CHECK                                    2          !2
         10      > JMPZ                                                     ~7, ->14
   12    11    >   NEW                                              $8      'stdClass'
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !2, $8
   14    14    >   FETCH_DIM_R                                      ~11     !0, !1
         15        IS_IDENTICAL                                             !2, ~11
         16      > JMPZ                                                     ~12, ->18
   15    17    > > RETURN                                                   <true>
   17    18    >   ASSIGN_DIM                                               !0
         19        OP_DATA                                                  !2
   18    20      > FE_RESET_RW                                      $14     !0, ->35
         21    > > FE_FETCH_RW                                              $14, !3, ->35
   19    22    >   TYPE_CHECK                                  128  ~15     !3
         23      > JMPZ_EX                                          ~15     ~15, ->28
         24    >   INIT_FCALL_BY_NAME                                       'is_cyclic'
         25        SEND_VAR_EX                                              !3
         26        DO_FCALL                                      0  $16     
         27        BOOL                                             ~15     $16
         28    > > JMPZ                                                     ~15, ->34
   20    29    >   INIT_FCALL                                               'array_pop'
         30        SEND_REF                                                 !0
         31        DO_ICALL                                                 
   21    32        FE_FREE                                                  $14
         33      > RETURN                                                   <true>
   18    34    > > JMP                                                      ->21
         35    >   FE_FREE                                                  $14
   24    36        INIT_FCALL                                               'array_pop'
         37        SEND_REF                                                 !0
         38        DO_ICALL                                                 
   25    39      > RETURN                                                   <false>
   26    40*     > RETURN                                                   null

End of function is_cyclic

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.87 ms | 1458 KiB | 18 Q