I'm trying to organize a multidimensional array (just one) so that a child array always looks like your parent's "children", given your id.
If there are duplicates, it should join (so that only one is left).
I have tried to mix functions like array_search, array_key_exists, array_values, etc. but without success.
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 issue, but I'm not able to implement it.
How would you solve the problem?