views:

24

answers:

2

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'
            ),
          )),
+1  A: 

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.

Gordon
A: 

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"]
BudgieInWA
That's what I thought but it isn't.
What do you get when you `print_r($heirarchy)`?
BudgieInWA
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.