php - 通过依赖项迭代数组和顺序

时间:2015-02-25 21:31:54

标签: php dependencies

我想知道是否有已知的类/函数或任何教程可以帮助按依赖性排序数组(存储为密钥对的值)。

即。 (请注意这些不是实际的依赖)

Array
(
    [blueimp-canvas-to-blob] => Array
        (
        )

    [blueimp-load-image] => Array
        (
            [0] => jquery
        )

    [blueimp-tmpl] => Array
        (
            [0] => jquery
            [1] => blueimp-load-image
        )

    [jquery] => Array
        (
        )

    [blueimp-file-upload] => Array
        (
            [0] => jquery
            [1] => blueimp-tmpl
            [2] => blueimp-load-image
            [3] => blueimp-canvas-to-blob
        )

)

将被命令为:

Array
(
    [blueimp-canvas-to-blob] => Array
        (
        )

    [jquery] => Array
        (
        )

    [blueimp-load-image] => Array
        (
            [0] => jquery
        )

    [blueimp-tmpl] => Array
        (
            [0] => jquery
            [1] => blueimp-load-image
        )

    [blueimp-file-upload] => Array
        (
            [0] => jquery
            [1] => blueimp-tmpl
            [2] => blueimp-load-image
            [3] => blueimp-canvas-to-blob
        )

)

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

管理以解决这个问题(我认为):

private function orderItems(){
    while( ! empty( $this->order ) ) {
        $item = array_splice( $this->order, 0, 1 );
        $dependencies = end( $item );
        $name = key( $item );
        if( empty( $dependencies ) ) {
            $this->ordered[$name] = $dependencies;
        } else {
            $satisafied = true;
            foreach( $dependencies as $dependency ) {
                if( isset( $this->order[$dependency] ) ) $satisafied = false;
            }
            if( $satisafied ) {
                $this->ordered[$name] = $dependencies;
            } else {
                $this->order = $this->order + $item;
            }
        }
    }
}

如果其他人想要一些类似的东西