Answer the question
In order to leave comments, you need to log in
How to sort an array with dependencies?
There is an array inside which elements can depend on each other. For example, like this:
$items = [
'item_a' => ['require' => ['item_b']], // этот элемент зависит от 'item_b'
'item_b' => ['require' => []], // этот элемент не зависит от других
'item_c' => ['require' => ['item_a']],
'item_d' => ['require' => ['item_a', 'item_e']], // этот элемент зависит сразу от двух других - 'item_a' и 'item_b'
'item_e' => ['require' => ['item_a']],
];
Answer the question
In order to leave comments, you need to log in
How do you propose to sort this?
$items = [
'item_a' => ['require' => ['item_b']],
'item_b' => ['require' => ['item_с']],
'item_c' => ['require' => ['item_a']],
];
foreach (array_keys($items) as $key) {
$items[$key]['name'] = $key;
}
uasort($items, function($a, $b) {
return (in_array($b['name'], $a['require']) || empty($b['require']));
});
/**
* @param array $items
* @return bool
*/
function search_cycle($items)
{
$search = function(array $path) use ($items, &$search)
{
$count = count($path);
$first = $path[0];
$last = $path[$count - 1];
if ($count > 1 && in_array($last, array_slice($path, 0, $count - 1))) {
return true;
}
foreach ($items[$last]['require'] as $key) {
if ($search(array_merge($path, [$key]))) {
return true;
}
}
return false;
};
foreach ($items as $key => $data) {
if ($search([$key])) {
return true;
}
}
return false;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question