$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' => 6038, 'parent' => 6036, 'name' => 'Amy Martin')
);
$temp = array(
array('id' => 5273, 'name' => 'John Doe'),
array('id' => 6032, 'name' => 'John Doe -> Sally Smith'),
array('id' => 6034, 'name' => 'John Doe -> Sally Smith -> Mike Jones'),
array('id' => 6035, 'name' => 'Jason Williams'),
array('id' => 6036, 'name' => 'Jason Williams -> Sara Johnson'),
array('id' => 6037, 'name' => 'Jason Williams -> Dave Wilson'),
array('id' => 6038, 'name' => 'Jason Williams -> Sara Johnson -> Amy Martin')
);
$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' => 6038, 'parent' => 6036, 'name' => 'Amy Martin')
);
function getName($storage, $id, $separator = ' -> ', $result = array()) {
$found = null;
foreach ($storage as $item) {
if ($item['id'] == $id) {
$found = $item;
break;
}
}
if ( ! is_null($found) ) {
$result[] = $found['name'];
if ($found['parent'] != 0) {
return getName($storage, $found['parent'], $separator, $result);
}
return implode($separator, array_reverse($result));
}
return '';
}
function breadcrumbs($items) {
$result = array();
foreach ($items as $item) {
$result[] = array(
'id' => $item['id'],
'name' => getName($items, $item['id'])
);
}
return $result;
}
var_dump(breadcrumbs($temp));
$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));