基于元素依赖的PHP Order数组 [英] PHP Order array based on elements dependency
问题描述
相当困难的一个解释,但实际上我有一系列具有ID的项目,其中可以包含其他数组项目的ID列表。例如
$ items = [
[id:'one',deps:['three']]
[id:'two'],
[id:'three',deps:['four','two']],
[id:'four']
];
所以你可以看到这里,一个取决于三个,三个取决于四和二。 / p>
我需要获得一个新的数组,它们按顺序包含这些项目,以便依次列出。所以上面的数组将转换成
$ items = [
[id:'four'],
[id:'two'],
[id:'three',deps:['four','two']],
[id:'one',deps:['three' ]]
];
我该如何完成?我已经尝试过各种while循环检查项目位置,但不能破解。
谢谢
更新有些人表示重复的问题 您可以使用这样的函数,它会遍历所有依赖关系,否则可以解决更多依赖关系: Fairly hard one to explain, but effectively I've got an array of items which have IDs, of which can contain a list of IDs for other array items. for example So as you can see here, one depends on three, and three depends on four and two. I need to get a new array, that contains these items in order - so that the dependencies are listed in order. So the above array would convert into How would I complete this? I've tried various while loops checking for item positions, but can't crack it. Thanks UPDATE Some people have said its a duplicate question of THIS but the main difference being the above example has multiple dependencies - whereas the mentioned thread only works on a single string dependency you can use a function like this, that iterates until all dependencies are met, or no more dependencies can be resolved:
这篇关于基于元素依赖的PHP Order数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ items = array(array('id'=>'one','deps'=> array('three')),
array('id'=>两个'),
数组('id'=>'三','deps'=>数组('四','二')),
数组('id'=> '四'));
$ sortedItems = sortDeps($ items);
var_dump($ sortedItems);
函数sortDeps($ items){
$ res = array();
$ doneList = array();
//而不是所有的项目都解决了:
while(count($ items)> count($ res)){
$ doneSomething = false;
foreach($ items as $ itemIndex => $ item){
if(isset($ doneList [$ item ['id']])){
//已经在结果集中的项目
继续;
}
$ resolved = true;
if(isset($ item ['deps'])){
foreach($ item ['deps'] as $ dep){
if(!isset($ doneList [$ dep])){
//有不符合的依赖项:
$ resolved = false;
break;
}
}
}
if($ resolved){
//满足所有依赖关系:
$ doneList [$ item ['id'] ] = true;
$ res [] = $ item;
$ doneSomething = true;
}
}
if(!$ doneSomething){
echo'unresolvable dependency';
}
}
return $ res;
}
$items = [
[id: 'one', deps: ['three']],
[id: 'two'],
[id: 'three', deps: ['four', 'two']],
[id: 'four']
];
$items = [
[id: 'four'],
[id: 'two'],
[id: 'three', deps: ['four', 'two']],
[id: 'one', deps: ['three']]
];
$items = array(array('id' => 'one', 'deps' => array('three')),
array('id' => 'two'),
array('id' => 'three', 'deps' => array('four', 'two')),
array('id' =>'four'));
$sortedItems = sortDeps($items);
var_dump($sortedItems);
function sortDeps($items) {
$res = array();
$doneList = array();
// while not all items are resolved:
while(count($items) > count($res)) {
$doneSomething = false;
foreach($items as $itemIndex => $item) {
if(isset($doneList[$item['id']])) {
// item already in resultset
continue;
}
$resolved = true;
if(isset($item['deps'])) {
foreach($item['deps'] as $dep) {
if(!isset($doneList[$dep])) {
// there is a dependency that is not met:
$resolved = false;
break;
}
}
}
if($resolved) {
//all dependencies are met:
$doneList[$item['id']] = true;
$res[] = $item;
$doneSomething = true;
}
}
if(!$doneSomething) {
echo 'unresolvable dependency';
}
}
return $res;
}