So I have the following:
id => the id of the folder
name => the name of the folder
folder_id => null if it's in the root, but will have the "id" of it's parent if it's inside another folder
I have all of these listed out in an array like so: (this will be $folder_array
in my example)
Array (
[1] => Array (
[name] => folder1
[id] => 1
[folder_id] => null
)
[2] => Array (
[name] => folder2
[id] => 2
[folder_id] => null
)
[3] => Array (
[name] => folder3
[id] => 3
[folder_id] => 2
)
[4] => Array (
[name] => folder4
[id] => 4
[folder_id] => 3
)
}
I am trying to make an array that has a folder tree of sorts, so I'd like it to look like:
Array (
[1] => Array (
[name] => folder1
[id] => 1
[folder_id] => null
)
[2] => Array (
[name] => folder2
[id] => 2
[folder_id] => null,
[children] => Array (
[3] => Array (
[name] => folder3
[id] => 3
[folder_id] => 2,
[children] => Array (
[4] => Array (
[name] => folder4
[id] => 4
[folder_id] => 3
)
)
)
)
)
}
So far I can get the first level of folders into it's right children array, but I'm having trouble getting multiple levels to go in.
Could anyone help me fix up this code so that I can make an effective folder tree. Also, if anyone has any more effective ways of organizing, I'd be open to that as well.
This is my code so far:
$folder_array = array(
"1" => array("name"=> "folder1","id" => "1","folder_id" => null),
"2" => array("name"=> "folder2","id" => "2","folder_id" => null),
"3" => array("name"=> "folder3","id" => "3","folder_id" => "2"),
"4" => array("name"=> "folder4","id" => "4","folder_id" => "3")
);
//a new array to store the folder tree in
$new_array = array();
//now search through each one that has no folder_id
foreach($folder_array as $folder)
{
//if the folder id is empty, it means it is in the root(home) folder
if(empty($folder['folder_id']))
{
$new_array[$folder['id']] = $folder_form_array[$folder['id']];
//now go through folder_array again and see if it has any folders inside that one
foreach($folder_array as $folder2)
{
//..and so on
}
}
}