tags:

views:

148

answers:

3

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?

A: 
$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
A: 

Hi,

Thanks for the answer. This looks nice, but what i need is an array which has the structure inside.

Do you have an idee?

regards

That script produces an array... $tree _IS_ an array. $tree['value'] is an array with the complete set of entries from domain.com to 2.jsp. $tree['level'] is a helper array showing the indent for each entry.Now perhaps if you specified the exact format that you wanted (as people commented to your original question). Nor have you even specified whether your list of urls is an array, or a comma-separated string, or what. If you want somebody to provide exactly what you want... you have to help by telling us exactly what you want.
Mark Baker
+1  A: 

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