3v4l.org

run code in 300+ PHP versions simultaneously
<?php function insertNewNodeTest() { $array = array( 'a' => array( 'a_1' => 'a_1 text', ), ); $object = json_decode( json_encode( $array )); //** $object = new ArrayObject( $array, 0, "RecursiveArrayIterator" ); $iterator = new RecursiveIteratorIterator( new RecursiveArrayIterator( $object ), RecursiveIteratorIterator::CHILD_FIRST ); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// foreach( $iterator as $key => $current ) { // insert a new empty node if( $key == 'a_1' ) { $iterator->getInnerIterator()->offsetSet( 'a_2', new stdClass() ); } // add new leaf into the previously inserted node if( $key == 'a_2' ) { $current->ex_empty = 'not empty now'; } // add another leaf into the previously inserted node if( $key == 'a_2' ) { $current->second_leaf = 'leaf with property'; } // add third leaf WITH ITERATOR(!) instead of using the $current->third_leaf if( $key == 'a_2' && $iterator->getInnerIterator()->hasChildren() ) { $iterator->getInnerIterator()->getChildren()->offsetSet( 'third_leaf', 'leaf with iterator'); } } print_r($object); } insertNewNodeTest();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4D63R
function name:  (null)
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   INIT_FCALL                                               'insertnewnodetest'
          1        DO_FCALL                                      0          
          2      > RETURN                                                   1

Function insertnewnodetest:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 54
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 54
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 29
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 33
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
2 jumps found. (Code = 46) Position 1 = 39, Position 2 = 44
Branch analysis from position: 39
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 53
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 53
Branch analysis from position: 44
Branch analysis from position: 37
Branch analysis from position: 33
Branch analysis from position: 29
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 54
filename:       /in/4D63R
function name:  insertNewNodeTest
number of ops:  59
compiled vars:  !0 = $array, !1 = $object, !2 = $iterator, !3 = $current, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, <array>
   13     1        INIT_FCALL                                               'json_decode'
          2        INIT_FCALL                                               'json_encode'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5        SEND_VAR                                                 $6
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !1, $7
   15     8        NEW                                              $9      'RecursiveIteratorIterator'
          9        NEW                                              $10     'RecursiveArrayIterator'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
         12        SEND_VAR_NO_REF_EX                                       $10
         13        SEND_VAL_EX                                              2
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !2, $9
   21    16      > FE_RESET_R                                       $14     !2, ->54
         17    > > FE_FETCH_R                                       ~15     $14, !3, ->54
         18    >   ASSIGN                                                   !4, ~15
   25    19        IS_EQUAL                                                 !4, 'a_1'
         20      > JMPZ                                                     ~17, ->29
   27    21    >   INIT_METHOD_CALL                                         !2, 'getInnerIterator'
         22        DO_FCALL                                      0  $18     
         23        INIT_METHOD_CALL                                         $18, 'offsetSet'
         24        SEND_VAL_EX                                              'a_2'
         25        NEW                                              $19     'stdClass'
         26        DO_FCALL                                      0          
         27        SEND_VAR_NO_REF_EX                                       $19
         28        DO_FCALL                                      0          
   32    29    >   IS_EQUAL                                                 !4, 'a_2'
         30      > JMPZ                                                     ~22, ->33
   34    31    >   ASSIGN_OBJ                                               !3, 'ex_empty'
         32        OP_DATA                                                  'not+empty+now'
   39    33    >   IS_EQUAL                                                 !4, 'a_2'
         34      > JMPZ                                                     ~24, ->37
   41    35    >   ASSIGN_OBJ                                               !3, 'second_leaf'
         36        OP_DATA                                                  'leaf+with+property'
   46    37    >   IS_EQUAL                                         ~26     !4, 'a_2'
         38      > JMPZ_EX                                          ~26     ~26, ->44
         39    >   INIT_METHOD_CALL                                         !2, 'getInnerIterator'
         40        DO_FCALL                                      0  $27     
         41        INIT_METHOD_CALL                                         $27, 'hasChildren'
         42        DO_FCALL                                      0  $28     
         43        BOOL                                             ~26     $28
         44    > > JMPZ                                                     ~26, ->53
   48    45    >   INIT_METHOD_CALL                                         !2, 'getInnerIterator'
         46        DO_FCALL                                      0  $29     
         47        INIT_METHOD_CALL                                         $29, 'getChildren'
         48        DO_FCALL                                      0  $30     
         49        INIT_METHOD_CALL                                         $30, 'offsetSet'
         50        SEND_VAL_EX                                              'third_leaf'
         51        SEND_VAL_EX                                              'leaf+with+iterator'
         52        DO_FCALL                                      0          
   21    53    > > JMP                                                      ->17
         54    >   FE_FREE                                                  $14
   53    55        INIT_FCALL                                               'print_r'
         56        SEND_VAR                                                 !1
         57        DO_ICALL                                                 
   55    58      > RETURN                                                   null

End of function insertnewnodetest

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.87 ms | 1394 KiB | 20 Q