从文件夹列表中递归创建文件夹数组

时间:2012-04-17 14:13:09

标签: php arrays recursion

我在从文件夹数组创建类似文件夹结构的数组时遇到了问题。

这是函数接收的数组

Array
(
[0] => Array
    (
        [id] => 6
        [name] => 2011
        [parent_folder] => 1
    )

[1] => Array
    (
        [id] => 5
        [name] => 2012
        [parent_folder] => 1
    )

[2] => Array
    (
        [id] => 7
        [name] => April
        [parent_folder] => 6
    )

[3] => Array
    (
        [id] => 2
        [name] => Folder2
        [parent_folder] =>
    )

[4] => Array
    (
        [id] => 1
        [name] => Folder1
        [parent_folder] =>
    )
)

输出应该是一个数组,其子文件夹作为数组存储在partent_folder数组中的键'content'下

Array
(
[0] => Array
    (
        [id] => 2
        [name] => Folder2
        [parent_folder] =>
    )

[1] => Array
    (
        [id] => 1
        [name] => Folder1
        [parent_folder] =>
        [content] => Array
            (
                [0] => Array
                    (
                        [id] => 6
                        [name] => 2011
                        [parent_folder] => 1
                        [content] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 7
                                        [name] => April
                                        [parent_folder] => 6
                                    )
                            )
                    )

                [1] => Array
                    (
                        [id] => 5
                        [name] => 2012
                       [parent_folder] => 1
                    )

            )
    )
)

我试过但却无法让它发挥作用。必须有一种递归方式来重新排列数组。有人可以帮忙吗? 非常感谢!

3 个答案:

答案 0 :(得分:1)

假设你的原始数组叫做

$tree;

其中的每个元素都有一个数组,并有三个命名条目:

  1. id - 代表自己 - 进一步称为$idName
  2. name - 一些字符串 - 进一步称为$parentName
  3. parent - 代表父级 - 进一步调用$childrenName
  4. 要从平面移植到树中,每个孩子必须能够解决它的父母。因此,创建一个临时数组,该数组的id为每个树元素的别名。我很快将其命名为$t并创建它:

    foreach ($tree as $k => $v)
        $t[$v[$idName]] = &$tree[$k];
    

    由于这个别名表,现在可以将子项分配给它的父项(如果父项不是FALSE),重新安排非常简单:

    foreach ($tree as $k => $v)
        if (($p = $v[$parentName]) && ($t[$p][$childrenName][] = &$t[$v[$idName]]))
            unset($tree[$k]);
    

    完成此操作后,不再需要alias-table,可以取消设置:

     unset($t);
    

    瞧,树准备好了:

     var_dump($tree);
    

    输出:

    array(2) {
      [3]=> array(3) {
        ["id"]=> string(1) "2"
        ["name"]=> string(7) "Folder2"
        ["parent_folder"]=> NULL
      }
      [4]=> array(4) {
        ["id"]=> string(1) "1"
        ["name"]=> string(7) "Folder1"
        ["parent_folder"]=> NULL
        ["content"]=> array(2) {
          [0]=> array(4) {
            ["id"]=> string(1) "6"
            ["name"]=> string(4) "2011"
            ["parent_folder"]=> string(1) "1"
            ["content"]=> array(1) {
              [0]=> array(3) {
                ["id"]=> string(1) "7"
                ["name"]=> string(5) "April"
                ["parent_folder"]=> string(1) "6"
              }
            }
          }
          [1]=> array(3) {
            ["id"]=> string(1) "5"
            ["name"]=> string(4) "2012"
            ["parent_folder"]=> string(1) "1"
          }
        }
      }
    }
    

答案 1 :(得分:0)

您需要设置如下内容:

function look_through_array($array) {
for ($i=0; $i<sizeof($array); $i++) {
    if (is_array($array[$i]))
        look_through_array($array[$i]);
    else
        create_folder($array[$i]);
}
}

function create_folder($array) {
    # make a folder
}

答案 2 :(得分:0)

我知道这个问题已经过时但提供的答案对我不起作用。解决方案的第一部分源于hakre的答案。这就是我最终做的事情:

global $t;

foreach ($tree as $key => $value){
    $idName = $value['id'];
    $t[$idName] = $tree[$key];
}

foreach ($t as $key => $value){
    //echo $value['name'] . '<br />';
    $t[$key]['uri'] = recursiveArray($value).'/'.$value['name'];
    //echo '<br />';
}

function recursiveArray($value) {
    global $t;    

        if ($value['parentId'] != 0){            
            $parentName = $t[$value['parentId']]['name'];
            //$parentId = $value['parentId'];
            return recursiveArray($t[$value['parentId']]).'/'.$parentName;
        }        
        return '';
}

tree是一个看起来就像OP数组的数组。结果是明显不同的是,不是嵌套数组,而是为每个数组以uri值返回文件的嵌套。

[76] => Array
    (
        [id] => 76
        [parentId] => 17
        [name] => Evite Templates
        [uri] => /Resources/Tools and Forms/General Tools and Forms/Countdown Events/Evite Templates
    )

[79] => Array
    (
        [id] => 79
        [parentId] => 90
        [name] => Celebration Meeting
        [uri] => /Resources/Tools and Forms/General Tools and Forms/Celebration Meeting
    )

[80] => Array
    (
        [id] => 80
        [parentId] => 90
        [name] => Recruitment Meeting
        [uri] => /Resources/Tools and Forms/General Tools and Forms/Recruitment Meeting
    )