0

I have a Multi-dimensional array like this:

<pre>Array
(
    [0] => Array
        (
            [id] => 52a83521-0914-4264-8fd9-07d9c601692a
            [role_id] => 2
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 54c1f5e4-b52c-4e17-b1bf-1f4616091b4e
                            [role_id] => 8
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 54c20aba-201c-40ce-b3df-22d516091b4e
                                            [role_id] => 9
                                        )

                                    [1] => Array
                                        (
                                            [id] => 54c20f4b-6e44-40ec-ae22-223a16091b4e
                                            [role_id] => 9
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 54c1f8bb-ebac-466b-a83f-13a416091b4e
                            [role_id] => 8
                        )

                )

        )

)
</pre>

I need to get populate all role_id from this array in order. I tried using recursive function like this:

<?php
public function tree_check($tree){
  $tree_keys = $this->_recursion($tree);
  print_r($tree_keys);
}
public function _recursion($tree){
        foreach ($tree as $n => $v)
        {
            if(isset($v['role_id'])){
               $key_arr[] = $v['role_id']; 
            }
            if (is_array($v))
                $this->_recursion($v);

        }
        return $key_arr;
    }

I am expecting the following ouput:

<pre>
Array(
[0]=>2,
[1]=>8,
[2]=>9,
[3]=>9,
[4]=>8
)
</pre>

Here, I cannot achieve my expected output. The level of array may vary dynamically and that is why I have done it with recursive function.

How should I return the array from recursive function?

Since, I don't know how depth is my array, how can I find the last key value pair of array?

1
  • wat is current output? Commented Jan 26, 2015 at 18:30

1 Answer 1

4

You can go through the elements using a RecursiveIteratorIterator:

function listRolesRecursive($myArray) {
    $res = array();
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($myArray), RecursiveIteratorIterator::SELF_FIRST);
    foreach ($iterator as $k => $v) {
        if($k === "role_id") {
            $res[] = $v;
        }
    }
    return $res;
}

$res = listRolesRecursive($arr);
var_dump($res);

It should do the trick.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.