I'm parsing a list of URLs from a website and want to build a hierarchical tree of nested arrays.
What I have so far (which works) is below. Because I don't know how deep the levels will go, I'm doing a simple check on the depth and then executing a basic push on to that node.
How can I rewrite this so any number of levels are accommodated?
$tree = array();
$tree[$domain] = array(); // this is the domain root
foreach ( $allMatches as $url ) {
$foo = parse_url($url );
// trim "/" from beginning and end
$bar = trim($foo['path'],'/');
// for every "/", add a level
$parts = explode('/', $bar);
$parts = array_filter($parts, 'strlen');
// note: there is likely a bug in here.
// If I process page-1/page-1-1 before page-1,
// then the leaf or branch containing page-1-1 will be deleted
if (count($parts) == 1){
$tree[$domain][$parts[0]] = array();
}
if (count($parts) == 2){
$tree[$domain][$parts[0]][$parts[1]] = array();
}
if (count($parts) == 3){
$tree[$domain][$parts[0]][$parts[1]][$parts[2]] = array();
}
if (count($parts) == 4){
$tree[$domain][$parts[0]][$parts[1]][$parts[2]][$parts[3]] = array();
}
};
These are the input URLs:
domain.com/page-1
domain.com/page-1/page-1-1
domain.com/page-1/page-1-1/page-1-1-1
domain.com/page-1/page-1-2
domain.com/page-1/page-1-1/page-1-2-1
domain.com/page-2
domain.com/page-2/page-2-1
Note: I do not necessarily need to have domain.com/page-2 in the list in order to generate a leaf for domain.com/page-2/page-2-1
This is the desired resulting structure:
Array
(
[domain.com] => Array
(
[page-1] => Array
(
[page-1-1] => Array
(
[page-1-1-1] => Array
(
)
)
[page-1-2] => Array
(
[page-1-2-1] => Array
(
)
)
)
[page-2] => Array
(
[page-2-1] => Array
(
)
)
)
)
page-2in the list, but you do havepage-2/page-2-1do you want to create the parent node (i.e.page-2), or do you only wantpage-1-2as a leaf node?