my problem is very simple, I'll try to explain with example. I have an array (unsorted) like so:
$nodes = [
['id' => 1, 'dest_id' => 2],
['id' => 2, 'dest_id' => 3],
['id' => 3, 'dest_id' => null],
['id' => 4, 'dest_id' => 5],
['id' => 5, 'dest_id' => null],
... etc
]
So basically nodes have an id and can have a destination which is the id of another node. Destination can be null. No node can have the same destination. I'm looking for an algorithm that could output this:
$paths = [[1, 2, 3], [4, 5]]
As you can see, in the output, the nodes that form a path are ordered so that node 2 which is destination of node 1 is placed after node 1.
Any help? Thank you.