I'm having a very difficult time figuring out how I can recursively iterate through an array and create an array of it's "path"
I have two arrays. One is an array of a directory tree that looks something like this:
$directoryTree = [
'accounting' => [
'documents' => [
],
'losses' => [
],
'profit' => [
]
],
'legal' => [
'documents' => [
]
]
];
Another is a list of files that specify which directory "path" the file should reside:
$fileList = [
[
'name' => 'Overview.doc',
'dir_path' => []
],
[
'name' => 'Incorporation.doc',
'dir_path' => []
],
[
'name' => 'Profit And Loss.xls',
'dir_path' => ['accounting']
],
[
'name' => 'Profit 1.xls',
'dir_path' => ['accounting', 'profit']
],
[
'name' => 'Loss 1.xls',
'dir_path' => ['accounting', 'losses']
],
[
'name' => 'TOS Draft.doc',
'dir_path' => ['legal', 'documents']
]
[
'name' => 'Accounting Doc.pdf',
'dir_path' => ['accounting', 'documents']
],
];
Essentially what I am trying to do is iterate through the $directoryTree
and see if there are any elements in the $fileList
that has the "path" where the iterator is. If there is the element should be added there.
The final array should look something like this:
$finalOutput = [
'accounting' => [
'documents' => [
'Accounting Doc.pdf'
],
'losses' => [
'Loss 1.xls'
],
'profit' => [
'Profit 1.xls'
],
'Profit And Loss.xls'
],
'legal' => [
'documents' => [
'TOS Draft.Doc'
]
],
'Overview.doc',
'Incorporation.doc',
];
I'm also including this image just to clarify further:
The attempt I've made haven't really gotten me very far. I keep getting stuck while trying to traverse the array recursively and am not sure how to approach the problem next.