I'm using PHP 7.3. I want to create a nested array that each item must contain its own ancestors.
Original Array:
[
[
id => 1,
parentId => ""
],
[
id => 2,
parentId => 1
],
[
id => 3,
parentId => 2
]
]
Required Array:
[
[
id => 1,
parentId => "",
ancestors => []
],
[
id => 2,
parentId => 1,
ancestors => [
[
id => 1,
parentId => "",
ancestors => []
],
]
],
[
id => 3,
parentId => 2,
ancestors => [
[
id => 1,
parentId => "",
ancestors => []
],
[
id => 2,
parentId => 1,
ancestors => [
[
id => 1,
parentId => "",
ancestors => []
],
]
],
]
]
]
I tried to use this solution, but I think this problem is different. Any help / guidance is greatly appreciated! Thanks.