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?