$temp = array(
array('id' => 5273, 'parent' => 0, 'name' => 'John Doe'),
array('id' => 6032, 'parent' => 5273, 'name' => 'Sally Smith'),
array('id' => 6034, 'parent' => 6032, 'name' => 'Mike Jones'),
array('id' => 6035, 'parent' => 0, 'name' => 'Jason Williams'),
array('id' => 6036, 'parent' => 6035, 'name' => 'Sara Johnson'),
array('id' => 6037, 'parent' => 6035, 'name' => 'Dave Wilson'),
array('id' => 7011, 'parent' => 6036, 'name' => 'John Smith'),
array('id' => 7012, 'parent' => 6037, 'name' => 'Sarah Green'),
array('id' => 7013, 'parent' => 7012, 'name' => 'Mike Black'),
array('id' => 7014, 'parent' => 0, 'name' => 'George Roberts'),
array('id' => 7015, 'parent' => 7011, 'name' => 'Daniel Johnson'),
array('id' => 7016, 'parent' => 7014, 'name' => 'Anna Lee'),
array('id' => 7017, 'parent' => 7012, 'name' => 'Martin Albright'),
array('id' => 7018, 'parent' => 7013, 'name' => 'Alex Logan'),
array('id' => 6038, 'parent' => 6036, 'name' => 'Amy Martin')
);
function build_sorter($key) {
return function ($a, $b) use ($key) {
if ($a[$key] == $b[$key]) {
return 0;
}
return ($a[$key] < $b[$key]) ? -1 : 1;
};
}
function breadcrumbs($items, $separator = ' -> ') {
$result = array();
usort($items, build_sorter('parent'));
foreach ($items as $item) {
$result[$item['id']] = array(
'id' => $item['id'],
'name' => $item['parent']
? $result[$item['parent']]['name'].$separator.$item['name']
: $item['name']
);
}
$result = array_values($result);
usort($result, build_sorter('name'));
return $result;
}
var_dump(breadcrumbs($temp));