0

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

这是函数接收的数组

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 数组中的键“内容”下

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
                    )

            )
    )
)

我试过了,但无法让它工作。必须有一种递归方式来重新排列数组。有人可以帮忙吗?将不胜感激!

4

3 回答 3

1

假设您的原始数组被称为

$tree;

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

  1. id- 代表自己 - 进一步称为$idName
  2. name- 一些字符串 - 进一步调用$parentName
  3. parent- 代表父母 - 进一步调用$childrenName

要将平面转换为树,每个孩子都必须能够解决它的父母。因此,创建了一个临时数组,该数组通过它的 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]);

完成此操作后,不再需要别名表并且可以取消设置:

 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"
      }
    }
  }
}
于 2012-04-17T18:49:29.697 回答
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
    )
于 2013-11-08T14:23:41.133 回答
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
}
于 2012-04-17T14:18:23.117 回答