I have an array like that:
<?php
array(
array(
'id' => 1,
'parent_id' => null
),
array(
'id' => 2,
'parent_id' => 1
),
array(
'id' => 3,
'parent_id' => null
),
array(
'id' => 4,
'parent_id' => 2
)
)
I need to sort it to every child comes after it's immediate parent. So, ID 4 item should come right after ID 2.
Thanks.
This working solution works with array_multisort().
Try with:
If you want
null
values to be at the end replace thatforeach
with:See result with:
print_r($data);
.Working solution : PHP has user define sorting function uasort I have used this to sort your array.