SPL RecursiveArrayIterator::offset(Un)Set has no effect on instance created with ArrayObject

[email protected] (NaMarPi)
Newsgroups php.general
Message-ID <[email protected]>
Hello All,


It seems to me that an instance of RecursiveIteratorIterator with ArrayObject has no effect
when calling RecursiveArrayIterator::offsetUnset or RecursiveArrayIterator::offsetSet methods.
But everything works fine when the iterator is created with a json_decoded - json_encoded array.

I expect the same behavior of these two iterators, but maybe I am wrong.
Any guidance would be appreciated.


Test script:
----------------------------------
function example() {

    $array = array (
            'name' => 'text',
            'items'    => array (
                    '0'    => array ( 'phrase' => 'text text', ),
                    '1'    => array ( 'phrase' => 'text text text', ),
                    'ctrl' => array( '0' => '1', ),
            ),
    );

    $array_object = json_decode( json_encode( $array ));
   
 $iterator     = new RecursiveIteratorIterator( new 
RecursiveArrayIterator( $array_object ), 
RecursiveIteratorIterator::CHILD_FIRST );

    // comment these two lines and see the nice execution
    $array_object = new ArrayObject( $array, 0, "RecursiveArrayIterator"  );
    $iterator     = new RecursiveIteratorIterator( $array_object->getIterator(), RecursiveIteratorIterator::CHILD_FIRST );

    foreach( $iterator as $key => $value ) {

        $array_copy   = $iterator->getInnerIterator()->getArrayCopy();
        $keys         = array_keys( $array_copy );
        $numeric_keys = array_filter( $keys, 'is_numeric' );

        if( count( $numeric_keys ) !== 0 && count( $keys  ) == count( $numeric_keys ) + 1 ) {

            $ctrl = $array_copy['ctrl'];
            $iterator->getInnerIterator()->offsetUnset( 'ctrl' );

            foreach( $iterator->getInnerIterator() as $ctrl_value => $value ) {

                if( in_array( $ctrl_value, $ctrl )) {
                    $iterator->getInnerIterator()->getChildren()->offsetSet( 'ctrl', $ctrl_value );
                } else {
                    $iterator->getInnerIterator()->getChildren()->offsetSet( 'ctrl', '' );
                }
            }
            // print_r( $iterator->getInnerIterator() );
        }
    }
    print_r( object_to_array( $array_object ));
    // print_r( iterator_to_array( $iterator ));
}

// helper method
function object_to_array( $object ) {
    if( ! is_array( $object ) && ! is_object( $object )) {
        return $object;
    }
    if( is_object( $object )) {
        $object = get_object_vars( $object );
    }
    return array_map( __FUNCTION__, $object );
}

example();


Expected result:
--------------------------
[11-Mar-2013 18:24:48 UTC] Array
(
    [name] => text
    [items] => Array
        (
            [0] => Array
                (
                    [phrase] => text text
                    [ctrl] => 
                )

            [1] => Array
                (
                    [phrase] => text text text
                    [ctrl] => 1
                )
        )
)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.