doulan4939 2016-09-26 20:21
浏览 45
已采纳

PHP Order数组基于元素依赖

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

$items = [
   [id: 'one', deps: ['three']],
   [id: 'two'],
   [id: 'three', deps: ['four', 'two']],
   [id: 'four']
];

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

$items = [
   [id: 'four'],
   [id: 'two'],
   [id: 'three', deps: ['four', 'two']],
   [id: 'one', deps: ['three']]
];

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

  • 写回答

1条回答 默认 最新

  • doukui9491 2016-09-26 22:17
    关注

    you can use a function like this, that iterates until all dependencies are met, or no more dependencies can be resolved:

    $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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码