I want to code a function to get parent’s children. get_children supposed to return an array of $this_parent_id children.
/* $pages array */
Array
(
[0] => Pages Object
(
[id_page] => 1
[str_title] => index
[id_parent] => 0
)
[1] => Pages Object
(
[id_page] => 10
[str_title] => download
[id_parent] => 0
)
[2] => Pages Object
(
[id_page] => 11
[str_title] => about us
[id_parent] => 1
)
[3] => Pages Object
(
[id_page] => 12
[str_title] => contact us
[id_parent] => 1
)
[4] => Pages Object
(
[id_page] => 13
[str_title] => members
[id_parent] => 1
)
)
I want to push child arrays into an array when condition is true.
print_r(get_children(1, $pages));
function get_children($this_parent_id, $family) {
foreach($family as $page) {
if ($page->id_parent == $this_parent_id) {
/* here I need to append $page to $temp_array
isset($temp_array) ? $temp_array = array($temp_array, (array)$page) : $temp_array = (array)$page; */
}
}
return $temp_array;
}
1 Answer