I am trying to organize a multidimensional array (just one) so that a child array is always as "children" of its parent, taking into account its id.
If there are duplicates, it should join (so that only one is left).
I've already tried mixing functions like array_search, array_key_exists, array_values, etc. but to no avail.
The array I have:
array
0 =>
array
'id' => 111
'name' => 'A'
'father' => 222
'children' =>
array
'id' => 333
'name' => 'B'
'father' => 111
'children' => null
1 =>
array
'id' => 111
'name' => 'A'
'father' => 222
'children' =>
array
'id' => 444
'name' => 'C'
'father' => 111
'children' => null
2 =>
array
'id' => 222
'name' => 'D'
'father' => 0
'children' => null
The array I want:
array
0 =>
array
'id' => 222
'name' => 'D'
'father' => 0
'children' =>
array
'id' => 111
'name' => 'A'
'father' => 222
'children' =>
array
'id' => 333
'name' => 'B'
'father' => 111
'children' = null
array
'id' => 444
'name' => 'C'
'father' => 111
'children' => null
A recursive method seems to me the best way to approach the question, but I can not implement it.
How would you solve the problem?