0

I'm having trouble creating a folder-structure-like array from an array of folder.

This is an array the function receives

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] =>
    )
)

The output should be an array where subfolders are stored as arrays under the key 'content' inside their partent_folder's array

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
                    )

            )
    )
)

I tried around but can't get it to work. There must be a recursive way to rearrange the array. Can anybody help? Would be greatly appreciated!

hakre
  • 193,403
  • 52
  • 435
  • 836
  • How are you getting your list of files? – RageD Apr 17 '12 at 14:15
  • possible duplicate of [How can I convert a series of parent-child relationships into a hierarchical tree?](http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tre) - and please don't paste `print_r`'s but `var_export`'s. – hakre Apr 17 '12 at 14:21

3 Answers3

1

Let's say your original array is called

$tree;

Each element in there is an array and has three named entries:

  1. id - representing itself - further on called $idName,
  2. name - some string - further on called $parentName,
  3. parent - representing parent - further on called $childrenName

To transpose from flat into tree, each children must be able to address it's parent. Therefore a temporary array is created that has an alias to every tree element by it's id. I name it shortly $t and create it:

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

As now children can be assigned to it's parent (if the parent is not FALSE) thanks to this alias table, re-arrangement is pretty straight forward:

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

After this has been done, the alias-table is not needed any longer and can be unset:

 unset($t);

An voila, the tree is ready:

 var_dump($tree);

Output:

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"
      }
    }
  }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
0

You need something set up like this:

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
}
Ozzy
  • 8,244
  • 7
  • 55
  • 95
0

I know this question is old but the answer provided did not work for me. The first part of the solution was derived from hakre's answer. This is what I ended up doing:

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 is an array that looks just like the OP's array. The result is sightly different in that instead of nested arrays, the nesting of the files is returned in a uri value for each array.

[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
    )
am71722
  • 129
  • 12