3v4l.org

run code in 300+ PHP versions simultaneously
<?php function gen() { $data = array(); $i = 0; for($i; $i < 10; $i++) { $data[$i] = array( 'indent' => 'test ' . $i, 'depth' => 1 ); } for($i; $i < 700; $i++) { $data[$i] = array( 'indent' => 'test ' . $i, 'depth' => 'x', 'parent_id' => rand(0, $i) ); } return $data; } echo serialize(gen()); /* $array = array( 55 => array( 'ident' => 'test 1', 'depth' => 1, ), 77 => array( 'parent_id' => 55, 'ident' => 'test 2', 'depth' => 2, ), 109 => array( 'parent_id' => 77, 'ident' => 'test 3', 'depth' => 3, ), 78 => array( 'parent_id' => 55, 'ident' => 'test 4', 'depth' => 2, ), 25 => array( 'ident' => 'test 5', 'depth' => 1, ) ); */ $array = unserialize('a:0:{}'); /* function v1($array) { foreach ($array as $key => &$sub) { if (isset($sub['parent_id'])) { $array[$sub['parent_id']]['children'][$key] = &$sub; } } unset($sub); // unset the reference to make sure to not overwrite it later... // now remove the entries with parents foreach ($array as $key => $sub) { if (isset($sub['parent_id'])) unset($array[$key]); } return $array; } function push_at_key($what, $what_key, $key, $array) { foreach($array as $k => $v) { if($key == $k) { $array[$k]['children'][$what_key] = $what; return $array; } } foreach($array as $k => $v) { $array[$k] = push_at_key($what, $what_key, $key, $v['children']); } return $array; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; } else { $out = push_at_key($sub, $key, $sub['parent_id'], $out); } } return $out; } //var_dump(v1($array)); //var_dump(v2($array)); */
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/D0Rf3
function name:  (null)
number of ops:  11
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   INIT_FCALL                                               'serialize'
          1        INIT_FCALL                                               'gen'
          2        DO_FCALL                                      0  $1      
          3        SEND_VAR                                                 $1
          4        DO_ICALL                                         $2      
          5        ECHO                                                     $2
   55     6        INIT_FCALL                                               'unserialize'
          7        SEND_VAL                                                 'a%3A0%3A%7B%7D'
          8        DO_ICALL                                         $3      
          9        ASSIGN                                                   !0, $3
  106    10      > RETURN                                                   1

Function gen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 3
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 12
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 12
Branch analysis from position: 25
Branch analysis from position: 12
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 3
Branch analysis from position: 11
Branch analysis from position: 3
filename:       /in/D0Rf3
function name:  gen
number of ops:  27
compiled vars:  !0 = $data, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
    6     1        ASSIGN                                                   !1, 0
    8     2      > JMP                                                      ->9
   10     3    >   CONCAT                                           ~5      'test+', !1
          4        INIT_ARRAY                                       ~6      ~5, 'indent'
   11     5        ADD_ARRAY_ELEMENT                                ~6      1, 'depth'
    9     6        ASSIGN_DIM                                               !0, !1
   11     7        OP_DATA                                                  ~6
    8     8        PRE_INC                                                  !1
          9    >   IS_SMALLER                                               !1, 10
         10      > JMPNZ                                                    ~8, ->3
   15    11    > > JMP                                                      ->23
   17    12    >   CONCAT                                           ~10     'test+', !1
         13        INIT_ARRAY                                       ~11     ~10, 'indent'
   18    14        ADD_ARRAY_ELEMENT                                ~11     'x', 'depth'
   19    15        INIT_FCALL                                               'rand'
         16        SEND_VAL                                                 0
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $12     
         19        ADD_ARRAY_ELEMENT                                ~11     $12, 'parent_id'
   16    20        ASSIGN_DIM                                               !0, !1
   19    21        OP_DATA                                                  ~11
   15    22        PRE_INC                                                  !1
         23    >   IS_SMALLER                                               !1, 700
         24      > JMPNZ                                                    ~14, ->12
   23    25    > > RETURN                                                   !0
   24    26*     > RETURN                                                   null

End of function gen

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.49 ms | 1394 KiB | 20 Q