I'm assuming my loop keeps looping and clearing my temp array, but not sure how to fix this. At the end, return is always empty.
How can I correctly return my temp
array?
Data set:
Array(
[0] => Array(
[0] => Array(
[id] => 55
[parent] => 49
)
[1] => Array(
[id] => 62
[parent] => 50
)
[2] => Array(
[id] => 64
[parent] => 51
)
)
[1] => Array(
[0] => Array(
[id] => 49
[parent] => 0
)
)
)
Function:
<?php
$patterns = function($array, $temp = array(), $index = 0, $parent = 0) use(&$patterns) {
if($index < count($array)) {
foreach($array[$index] as $sub) {
if($index == 0 || $parent == $sub['id']) {
$temp[$index] = $sub['id'];
$patterns($array, $temp, $index + 1, $sub['parent']);
}
}
}
if($index >= count($array) && $parent == 0) {
print_r($temp); // correct result does display here!
return $temp; // this return gives no return
}
};
print_r($patterns($dataset));
?>
print_r
returns Array ( [0] => 55 [1] => 49 )