[ create a new paste ] login | about

Link: http://codepad.org/h5BxRQEv    [ raw code | output | fork ]

PHP, pasted on Feb 2:
<?php

function ProcessPath($entry,$depth,&$current)
{
  if($depth<count($entry))
  {
    $key = $entry[$depth];
    if(!isset($current[$key]))$current[$key] = null;
    ProcessPath($entry,$depth+1,$current[$key]);
  }
}


$data = array(
  array('path' => 'foo/bar/baz'),
  array('path' => 'foo/pippo/bar/paperino'),
  array('path' => 'paperino/pippo/bar/paperino'),
  array('path' => 'foo/bar/baz/qux'),
  array('path' => 'foo/bar'),
  array('path' => 'foo/pippo/bar'),
  array('path' => 'bar/baz/foo'),
  array('path' => 'baz'),
  array('path' => 'foo/pippo/bar/paperino'),
);
$result = null;
foreach($data as $path)
{
  ProcessPath(explode("/",$path['path']),0,$result);
}

print_r($result);


Output:
Array
(
    [foo] => Array
        (
            [bar] => Array
                (
                    [baz] => Array
                        (
                            [qux] => 
                        )

                )

            [pippo] => Array
                (
                    [bar] => Array
                        (
                            [paperino] => 
                        )

                )

        )

    [paperino] => Array
        (
            [pippo] => Array
                (
                    [bar] => Array
                        (
                            [paperino] => 
                        )

                )

        )

    [bar] => Array
        (
            [baz] => Array
                (
                    [foo] => 
                )

        )

    [baz] => 
)


Create a new paste based on this one


Comments: