1

This is my array:

ArrayObject::__set_state(
    array( 'data' => 
        array ( 
            'key1' => '573779', 
            'key2' => 'REF12345', 
            'key3' => '2010-07-12', 
            'key4' => '0000-00-00', 
            'key5' => '2010-07-12', 
            'key6' => '21654',
            'key7' => '0', 
            'key8' => ArrayObject::__set_state(array( )),
            'key9' => ArrayObject::__set_state(array( )), 
            'key10' => array ( ), 
            'key11' => array ( ), 
        )
)

I'm interested in finding out out how to retrieve any of the values for a key using SPL. Any of the keys can have arrays within arrays so I'm unsure how to obtain a value. I thought I may be able to use ->offsetGet('') but this only retrieves from the top-most array.

3
  • If a key contains an array, do you need to be able to access any specific member of that subarray? Also, that's a lot of keys. Commented Jul 16, 2010 at 15:42
  • Give an example of what you're trying to do, even if it doesn't currently work. Commented Jul 16, 2010 at 15:59
  • Yes, I potentially need to be able to access the array of a key Commented Jul 18, 2010 at 20:13

1 Answer 1

3

ArrayObject implements ArrayAccess so you can simply access them with the regular Array Notation, e.g.

echo $arrayObject['key1'];

In addition, ArrayObjects are IteratorAggregates, so you can iterate over them with foreach and decorate them with any of the SPL iterators, including the RecursiveIteratorIterator

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.