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?

2
  • 2
    Could you make it clearer which are keys/indexes and which are values using the PHP-standardish key => value notation? Commented Jun 22, 2010 at 14:56
  • ... and what's with the formatting? Why does every line start with "? Commented Jun 22, 2010 at 14:57

2 Answers 2

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);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

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 : stackoverflow.com/a/2215496/1740412 and look at my comment.
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>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.