How would I sort or iterate a multi-dimensional array so that it forms a tree like structure (all children after each parent) such as:
Parent #1
Child #1
Child #2
Parent #2
Child #1
Parent #3
Child #1
Child #2
This is an example Array. It is important to note that the array is initially unsorted (items and their children do not appear in any specific order).
Array (
[0] => Array
(
[id] => 1
[content] => Parent #1
)
[1] => Array
(
[id] => 2
[content] => Parent #2
)
[2] => Array
(
[id] => 3
[content] => Parent #3
)
[3] => Array
(
[parent] => 1
[content] => Child #1
)
[4] => Array
(
[parent] => 1
[content] => Child #2
)
[5] => Array
(
[parent] => 3
[content] => Child #2
)
[6] => Array (
[parent] => 3
[content] => Child #1
)
[7] => Array (
[parent] => 2
[content] => Child #1
)
)
I apologize if I am completely wrong or asking something that is impossible. I have tried using usort but I have been unable to figure out the proper logic.