从其他数组php开始的order数组

时间:2016-06-30 05:22:05

标签: php joomla joomla3.5

使用JFolder:

$tree = JFolder::listFolderTree('myfolder');

列出这样的树结构:(忽略和忽略2是每个文件夹b,n,v内的子文件夹)

 Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [name] => b
            [fullname] => myfolder/b
            [relname] => myfolder/b
        )

    [1] => Array
        (
            [id] => 2
            [parent] => 1
            [name] => ignore
            [fullname] => myfolder/ignore
            [relname] => myfolder/ignore
        )

    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [name] => ignore2
            [fullname] => myfolder/ignore2
            [relname] => myfolder/ignore2
        )

    [3] => Array
        (
            [id] => 4
            [parent] => 0
            [name] => n
            [fullname] => myfolder/n
            [relname] => myfolder/n
        )

    [4] => Array
        (
            [id] => 5
            [parent] => 4
            [name] => ignore
            [fullname] => myfolder/ignore
            [relname] => myfolder/ignore
        )

    [5] => Array
        (
            [id] => 6
            [parent] => 4
            [name] => ignore2
            [fullname] => myfolder/ignore2
            [relname] => myfolder/ignore2
        )

    [6] => Array
        (
            [id] => 7
            [parent] => 0
            [name] => v
            [fullname] => myfolder/v
            [relname] => myfolder/v
        )

    [7] => Array
        (
            [id] => 8
            [parent] => 7
            [name] => ignore
            [fullname] => myfolder/ignore
            [relname] => myfolder/ignore
        )

    [8] => Array
        (
            [id] => 9
            [parent] => 7
            [name] => ignore2
            [fullname] => myfolder/ignore2
            [relname] => myfolder/ignore2
        )

)

所以我们可以看到文件夹的顺序:0有名字" b" 1的名字是" n"和2有名字" v"。

现在我想从数组中按用户定义的顺序对它们进行排序:

$user_defined_array = [b,v,n];

所以现在$ tree排序应该是0 => b, 1 => v and 2 => n,如果用户更改了像[n,v,b]这样的数组,那么排序应该是这样的。

例如,如果用户定义的数组如[b,v,n],则生成的数组应如下所示:

 Array
(
    [0] => Array
        (
            [id] => 1
            [parent] => 0
            [name] => b
            [fullname] => myfolder/b
            [relname] => myfolder/b
        )

    [1] => Array
        (
            [id] => 2
            [parent] => 1
            [name] => ignore
            [fullname] => myfolder/ignore
            [relname] => myfolder/ignore
        )

    [2] => Array
        (
            [id] => 3
            [parent] => 1
            [name] => ignore2
            [fullname] => myfolder/ignore2
            [relname] => myfolder/ignore2
        )



    [3] => Array
        (
            [id] => 4
            [parent] => 0
            [name] => v
            [fullname] => myfolder/v
            [relname] => myfolder/v
        )

    [4] => Array
        (
            [id] => 5
            [parent] => 4
            [name] => ignore
            [fullname] => myfolder/ignore
            [relname] => myfolder/ignore
        )

    [5] => Array
        (
            [id] => 6
            [parent] => 4
            [name] => ignore2
            [fullname] => myfolder/ignore2
            [relname] => myfolder/ignore2
        )

    [6] => Array
        (
            [id] => 7
            [parent] => 0
            [name] => n
            [fullname] => myfolder/n
            [relname] => myfolder/n
        )

    [7] => Array
        (
            [id] => 8
            [parent] => 7
            [name] => ignore
            [fullname] => myfolder/ignore
            [relname] => myfolder/ignore
        )

    [8] => Array
        (
            [id] => 9
            [parent] => 7
            [name] => ignore2
            [fullname] => myfolder/ignore2
            [relname] => myfolder/ignore2
        )

)

我希望问题很明确,我希望有办法。

1 个答案:

答案 0 :(得分:1)

您可以在我理解您的问题时执行此操作

$user_defined_array = array('b','v','n');
$newarr = array();
$i=0;$k=1;
foreach($user_defined_array as $a){
        foreach($tree as $b){
                if($k==0 && !in_array($b['name'],$user_defined_array)){
                        $newarr[$i] = $b;$i++;
                }else{$k=1;}
                if($b['name']==$a) {
                        $newarr[$i] = $b;
                        $k=0;$i++;
                }

        }

}
var_dump($newarr);

这将根据您的新用户定义数组输出一个数组。