I need a little help. My knowledge of algorithms are weak. I can not write a recursive function in PHP that returns all the latest children.
Suppose our array looks like this:
Array
(
[0] => Array
(
[id_category] => 1
[name] => Accueil
[id_parent] => 0
)
[1] => Array
(
[id_category] => 2
[name] => Accessoires
[id_parent] => 1
)
[2] => Array
(
[id_category] => 3
[name] => Merchandising
[id_parent] => 1
)
[3] => Array
(
[id_category] => 4
[name] => Pièces détachées
[id_parent] => 1
)
[4] => Array
(
[id_category] => 5
[name] => Excavateur
[id_parent] => 4
)
[5] => Array
(
[id_category] => 6
[name] => série 100
[id_parent] => 5
)
[6] => Array
(
[id_category] => 7
[name] => above
[id_parent] => 6
)
[7] => Array
(
[id_category] => 8
[name] => système hydraulique
[id_parent] => 7
)
[8] => Array
(
[id_category] => 9
[name] => série 200
[id_parent] => 5
)
[9] => Array
(
[id_category] => 10
[name] => thru
[id_parent] => 6
)
[10] => Array
(
[id_category] => 11
[name] => Compaction
[id_parent] => 4
)
[11] => Array
(
[id_category] => 12
[name] => système électrique
[id_parent] => 7
)
)
I would like getLastChildren (5) or getLastChildren (6) or getLastChildren (7), the function returns me an answer array (“8”, “12”)
I will try to give an example.
If I take the category 5: = 6 and 9 are the children.
I look through children. The child 6 has two children, 7 and 10, Child 9: no children.
I put 9 in the list of children.
The child 7 has two children, 8 and 12. 8 has no children. 12 has no children. I add 8 and 12.
So we return (9,8,12) 10 has no children. I also added.
In the end I (9,8,12,10)
So what I would do, if I search “all the last children in category 7” => 8, and 12. I hope my explanation is “a little clearer.”
I would do something like that:
I split data and code, here is the code:
output: