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();
Output for git.master, git.master_jit, rfc.property-hooks
stdClass Object ( [a] => stdClass Object ( [a_1] => a_1 text [a_2] => stdClass Object ( [ex_empty] => not empty now [second_leaf] => leaf with property [third_leaf] => leaf with iterator ) ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
39.24 ms | 402 KiB | 8 Q