I'm twisting my brain trying to convert a multi-dimensional associative array into a multi-dimensional non-associative one and got to the point where I decided to seek help..
The whole story:
A. I've got a set of URLs and their KPIs (with an arbitrary folder depth):
URL, Visits
- www.example.com
- www.example.com/resource1, 100
- www.example.com/folderA/resource2, 200
- www.example.com/folderA/resource3, 300
- www.example.com/folderB/resource4, 400
B. With help of StackOverflow (and surprisingly few lines of PHP -> see here) I was able to transform this into a hierarchical array, representing the URL-structure as folders:
Array
(
[www.example.com] => Array
(
[folderA] => Array
(
[resource2] => Array
(
[visits] => 200
)
[resource3] => Array
(
[visits] => 300
)
)
[folderB] => Array
(
[resource4] => Array
(
[visits] => 400
)
)
[resource1] => Array
(
[visits] => 100
)
)
)
C. Now, however, I need to get this array into the following structure (children must be non-associative arrays) which is a real brain twister to me...
Array
(
[name] => www.example.com
[isFolder] => 1
[children] => Array
(
[0] => Array
(
[name] => folderA
[isFolder] => 1
[children] => Array
(
[0] => Array
(
[name] => resource2
[isFolder] => 0
[kpis] => Array
(
[visits] => 200
)
[children] => Array
(
)
)
[1] => Array
(
[name] => resource3
[isFolder] => 0
[kpis] => Array
(
[visits] => 300
)
[children] => Array
(
)
)
)
)
[1] => Array
(
[name] => folderB
[isFolder] => 1
[children] => Array
(
[0] => Array
(
[name] => resource4
[isFolder] => 0
[kpis] => Array
(
[visits] => 400
)
[children] => Array
(
)
)
)
)
[2] => Array
(
[name] => resource1
[isFolder] => 0
[kpis] => Array
(
[visits] => 100
)
[children] => Array
(
)
)
)
)
Can anyone help with an approach how to achieve this? Either by transforming the array from step B. or starting from scratch with the original URLs from step A... Any help is greatly appreciated! Thanks a lot!