从其他数组开始排序数组 php

order array as of other array php

使用 JFolder:

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

像这样列出树结构:(ignore 和 ignore2 是每个文件夹 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
        )

)

我希望问题很清楚,我希望有办法。

我理解你的问题,你可以这样做

$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);

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