function rec($array) {
foreach ( $array as $key => $value ) {
if ( !empty($value['parent']) ) {
if ( array_key_exists($value['parent'], $array) ) {
$array[$value['parent']]['parents'][] = array(
'title' => $value['title'],
'id' => $value['id'],
'parent' => $value['parent'],
'parents' => array()
);
unset($array[$key]);
}
}
}
return $array;
}
rec($array);