Function getting an array(s) to loop though to get all child categories of selected category. Array look something like:
Array (
[0] => Array (
[id] => 7
[title] => Deserts
[slug] => deserts
[child] => Array (
[0] => Array (
[id] => 8
[title] => Space
[slug] => space
[child] => Array (
[0] => Array (
[id] =>
[title] =>
[slug] =>
[child] => Array ( )
)
)
)
)
)
)
I come up with a code to display all the values with foreach() loop, but I can't make things work when I need data from an exact key, for example $category['id']
public static function recursiveChildCategory($categories = [], $depth = 0) {
// Loop through categories
foreach($categories as $category){
// If $category is an array.
if(is_array($category)){
// Drop empty arrays
$filteredCategory = array_filter($category);
// Loop
self::recursiveChildCategory($filteredCategory, $depth + 1);
} else {
//It is not an array, so print it out
echo str_repeat(" ", $depth), $category, '<br>';
}
}
}
currently variable $category just prints outs all values. I want to get all info separately, like $category['id'], $category['title'] and etc.
Thanks in advice
$category['id']instead of$category$categorycould already be the id or title etc.