0

I have a list of path (just strings), like this:

$data = array(
    array('path' => 'foo/bar/baz'),
    array('path' => 'foo/bar/baz/qux'),
    array('path' => 'foo/bar'),
    array('path' => 'bar/baz/foo'),
    array('path' => 'baz'),
);

I would like to achieve a structure like this

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

Side note

  1. the structure displays only, non-common portions
  2. the leaf would be null (null for me would be without children)

I know you will ask me what have you tried? the problem is: I don't know how to deal the problem in the right way

can you give me some advice, without massacre of downvote?

3
  • 1
    I don't see the pattern behind the array and the expected output. Because at the end you create: [baz] => but you already have one that begins with this. Commented Feb 2, 2015 at 19:16
  • @Rizier123 the leaf would be null (null for me would be without children) Commented Feb 2, 2015 at 19:18
  • You're trying to convert that array as it is into the desired array, right? Commented Feb 2, 2015 at 19:22

2 Answers 2

2

I got pretty close, but instead of the endings being null, they're array's with size 0.

<?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/bar/baz/qux'),
  array('path' => 'foo/bar'),
  array('path' => 'bar/baz/foo'),
  array('path' => 'baz'),
);
$result = null;
foreach($data as $path)
{
  ProcessPath(explode("/",$path['path']),0,$result);
}

print_r($result);

?>

output

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

                )

        )

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

        )

    [baz] => 
)

Essentially the function ProcessPath takes:

  • An array of the split path
  • The current depth eg: [foo] (0) or foo > [bar] (1) or foo > bar > [baz] (2)
  • A reference to where on the array the path is being put (denoted by &$reference

First off the function checks if the depth is within the current path being processed.

Then it extracts the $key to simplify the rest of the code.

This is where the magic happens, if the output hasn't got the current path segment set, it sets it.

The last line recurses onto the next element in the path $entry by taking the same $entry, increasing the $depth by 1, and changing the $current to the new section $current[$key].

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

5 Comments

If this doesn't work, just change [] to array() (could be a language error)
thank you very much. I edited the answer to support old versions of php
@SimoneNigro I just added a description to it so it probably would've cleared your edit. I added the array()s back in
Aight I redid my edit again (didn't know that you could just whack a null in there). Should give you the desired output now
is perfect, I tried it and it does exactly what I wanted
0

What you could do is iterate over the paths, explode the string of each path, and then add each value to the new array depending on the position of the value. If the value is the first, then it'll be the base. If it's the 2nd, then it goes in as a new array inside the first one, and so on.

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.