4

I need to build an tree (with arrays) from given urls.

I have the following list of urls:

http://domain.com/a/a.jsp
http://domain.com/a/b/a.jsp
http://domain.com/a/b/b.jsp
http://domain.com/a/b/c.jsp
http://domain.com/a/c/1.jsp
http://domain.com/a/d/2.jsp
http://domain.com/a/d/a/2.jsp

now i need an array like this:

domain.com
  a
    a.jsp
    b
      a.jsp
      b.jsp
      c.jsp
    c
      1.jsp
    d
      2.jsp
      a
        2.jsp

How can i do this with php?

danben
  • 80,905
  • 18
  • 123
  • 145
fr3ak4l
  • 41
  • 2

2 Answers2

2

i thought mark's solution was a bit complicated so here's my take on it:

(note: when you get to the filename part of the URI, I set it as both the key and the value, wasn't sure what was expected there, the nested sample didn't give much insight.)

<?php

$urls = array(
    'http://domain.com/a/a.jsp',
    'http://domain.com/a/b/a.jsp',
    'http://domain.com/a/b/b.jsp',
    'http://domain.com/a/b/c.jsp',
    'http://domain.com/a/c/1.jsp',
    'http://domain.com/a/d/2.jsp',
    'http://domain.com/a/d/a/2.jsp'
);

$array = array();

foreach ($urls as $url)
{
    $url = str_replace('http://', '', $url);
    $parts = explode('/', $url);

    krsort($parts);

    $line_array = null;
    $part_count = count($parts);

    foreach ($parts as $key => $value)
    {
        if ($line_array == null)
        {
            $line_array = array($value => $value);
        }
        else
        {
            $temp_array = $line_array;
            $line_array = array($value => $temp_array);
        }
    }

    $array = array_merge_recursive($array, $line_array);
}

print_r($array);

?>
joshtronic
  • 381
  • 3
  • 9
  • If you have a numerical segment into a URL (like `http://example.com/segment1/234/segment3`), `array_merge_recursive` will cause you some trouble because it will cast `234` as an integer. Please have a look there : http://stackoverflow.com/a/2215496/1740412 and look at my comment. – Link14 Jan 02 '15 at 23:31
0
$urlArray = array(  'http://domain.com/a/a.jsp',
                    'http://domain.com/a/b/a.jsp',
                    'http://domain.com/a/b/b.jsp',
                    'http://domain.com/a/b/c.jsp',
                    'http://domain.com/a/c/1.jsp',
                    'http://domain.com/a/d/2.jsp',
                    'http://domain.com/a/d/a/2.jsp'
                 );

function testMapping($tree,$level,$value) {
    foreach($tree['value'] as $k => $val) {
        if (($val == $value) && ($tree['level'][$k] == $level)) {
            return true;
        }
    }
    return false;
}

$tree = array();
$i = 0;
foreach($urlArray as $url) {
    $parsed = parse_url($url);
    if ((!isset($tree['value'])) || (!in_array($parsed['host'],$tree['value']))) {
        $tree['value'][$i] = $parsed['host'];
        $tree['level'][$i++] = 0;
    }
    $path = explode('/',$parsed['path']);
    array_shift($path);
    $level = 1;
    foreach($path as $k => $node) {
        if (!testMapping($tree,$k+1,$node)) {
            $tree['value'][$i] = $node;
            $tree['level'][$i++] = $level;
        }
        $level++;
    }
}


echo '<pre>';
for ($i = 0; $i < count($tree['value']); $i++) {
    echo str_repeat(' ',$tree['level'][$i]*2);
    echo $tree['value'][$i];
    echo '<br />';
}
echo '</pre>';
Mark Baker
  • 209,507
  • 32
  • 346
  • 385