0

I have a large arrayObject which I'm looping over using the following:-

$rit = new RecursiveIteratorIterator(new RecursiveArrayIterator($hierarchy));
foreach($rit as $key=> $val) {

}

How can I access the a specific key within the array? I can access them by echoing $key and the $val but I have specific keys I wish to access. If I attempt to call $key[''] I get the first letter on the key name.

Edit 1

Some sample data (There can be many different sub-children too):

ArrayObject::__set_state(array(
   0 => 
  ArrayObject::__set_state(array(
     0 => 
    array (
      'id' => '8755',
      'company_id' => '1437',
      'name' => 'Name 1'
    ),
     1 => 
    ArrayObject::__set_state(array(
       0 => 
      ArrayObject::__set_state(array(
         0 => 
        array (
          'id' => '8763',
          'company_id' => '1437',
          'name' => 'Name 2'
        ),
         1 => 
        ArrayObject::__set_state(array(
           0 => 
          ArrayObject::__set_state(array(
             0 => 
            array (
              'id' => '9067',
              'company_id' => '1437',
              'name' => 'Name 3'
            ),
          )),
           1 => 
          ArrayObject::__set_state(array(
             0 => 
            array (
              'id' => '8765',
              'company_id' => '1437',
              'name' => 'Name 4'
            ),
          )),
           2 => 
          ArrayObject::__set_state(array(
             0 => 
            array (
              'id' => '9049',
              'company_id' => '1437',
              'name' => 'Name 5'
            ),
          )),
           3 => 
          ArrayObject::__set_state(array(
             0 => 
            array (
              'id' => '8769',
              'company_id' => '1437',
              'name' => 'Name 6'
            ),
          )),
6
  • 1
    To help us to help you, what does $hierarchy->getArrayCopy() return? (i.e. show us the structure in array form, for us to work with your example) Also your question is not clear: do you want to access only the single item somewhere within the hierarchy, based on its "key"? (which will be what, its id number, some path/position?) Commented Oct 25, 2010 at 12:13
  • Can you please clarify what you are trying to achieve? In your comment an an answer below you said you wanted to "display the array as a representation on-screen". I'm afraid I don't understand what you are trying to do. Commented Oct 25, 2010 at 20:12
  • Sure. Basically this array is generated from an existing function. The array holds a structural representation of the relationships between departments. Commented Oct 26, 2010 at 8:41
  • What I'm trying to achieve is displaying this on-screen in a hierarchical format whereby the user can opt to check a department which selects all sub-departments so on and so on. Commented Oct 26, 2010 at 8:42
  • Let me get a copy of what $hierarchy->getArrayCopy() outputs. Commented Oct 26, 2010 at 8:42

2 Answers 2

1

By default, the RecursiveIteratorIterator will only list the leaves. Try

$rit = new RecursiveIteratorIterator(
           new RecursiveArrayIterator($hierarchy),
           RecursiveIteratorIterator::SELF_FIRST);

to get the containing elements as well.

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

Comments

0

Assuming $hierarchy is your large array, you can use $hierarchy["foo"] to access the value associated with the foo key. Or like this:

$my_key = "foo"
echo $hierarchy[$my_key]
// Same as
echo $hierarchy["foo"]

3 Comments

That's what I thought but it isn't.
What do you get when you print_r($heirarchy)?
Above is the original dump of $hierarchy before I pass it into RecursiveArrayIterator. What I'm trying to do is display the array as a representation on-screen with checkboxes next to each level in order to assign rights to each department.

Your Answer

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