0

Using JFolder:

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

Lists the tree structure like this: (ignore and ignore2 are subfolders inside each folder 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
        )

)

So we can see the ordering of the folders: 0 has name "b" 1 has name "n" and 2 has name "v".

Now I want to order them in user defined order from the array:

$user_defined_array = [b,v,n];

So now the $tree ordering should 0 => b, 1 => v and 2 => n and if the user changes the the array like [n,v,b] then ordering should be like that.

For example if user defined array like [b,v,n] the resulting array should look like this:

 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
        )

)

I hope question is clear and I hope there is a way.

4

1 回答 1

1

You can do this as I understand your question

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

This will output an array based on your new user defined array.

于 2016-06-30T06:32:16.457 回答