I've built a recursive PHP function to create a multidimensional array with parents/children. Now I want to create a function to create a list with paths to each array item. See the following array:
$testdata = [
['id' => 1, 'parent_id' => null, 'desc' => 'Vehicles'],
['id' => 2, 'parent_id' => 1, 'desc' => 'Cars'],
['id' => 3, 'parent_id' => 1, 'desc' => 'Motorbikes'],
['id' => 4, 'parent_id' => 1, 'desc' => 'Planes'],
['id' => 5, 'parent_id' => 2, 'desc' => 'Toyota'],
['id' => 6, 'parent_id' => 2, 'desc' => 'Volkswagen'],
['id' => 7, 'parent_id' => 2, 'desc' => 'Renault'],
['id' => 8, 'parent_id' => 3, 'desc' => 'Honda'],
['id' => 9, 'parent_id' => 3, 'desc' => 'Yamaha'],
['id' => 10, 'parent_id' => 4, 'desc' => 'Boeing'],
['id' => 11, 'parent_id' => null, 'desc' => 'Cities'],
['id' => 12, 'parent_id' => 11, 'desc' => 'Amsterdam'],
['id' => 13, 'parent_id' => 11, 'desc' => 'New York'],
];
Now the expected result should be like this:
$output = [
'Vehicles'
'Vehicles\Cars'
'Vehicles\Cars\Toyota'
'Vehicles\Cars\Volkswagen'
'Vehicles\Cars\Renault'
'Vehicles\Motorbikes'
'Vehicles\Motorbikes\Honda'
'Vehicles\Motorbikes\Yamaha'
'Vehicles\Planes'
'Vehicles\Planes\Boeing'
'Cities'
'Cities\Amsterdam'
'Cities\New York'
];
I have tried to rebuild my recursive function, but I can't wrap my head around it. Could you guys give me some advice or a push me in the right direction?