1

Hello just looking for some help as I've gotten stuck

I have two Strings:

C:\Users\Bob\My Documents
/Users/Bob/Documents

That gets put through

preg_split('/(?<=[\/\\\])(?![\/\\\])/', $string)

that returns

Array
(
    [0] => C:\
    [1] => Users\
    [2] => Bob\
    [3] => My Documents
)

Array
(
    [0] => /
    [1] => Users/
    [2] => Bob/
    [3] => Documents
)

I need

Array
(
    [C:\] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [My Documents] => array()
                        )

                )

        )

)

Array
(
    [/] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [Documents] => array()
                        )

                )

        )

)

And ultimately merged to

Array
(
    [C:\] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [My Documents] => array()
                        )

                )

        )
    [/] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [Documents] => array()
                        )

                )

        )

)

(properly merged, not just appended, so if another string started with C:\Users\Dan Then dan would appear on the ?3rd? Dimension. array_merge_recursive() ? )

3
  • 1
    what is the need to have that weird structure? Commented Oct 4, 2011 at 17:24
  • starting from a path and having an array of array of array of array... why you need that kind of result? what step next? seriously i don't think that's a good way to use arrays Commented Oct 4, 2011 at 17:44
  • Well it makes sense in my own head. As you traverse down a path you get deeper into the folder structure. Why not get deeper into the series of arrays? In this context an array is like a folder, can contain more folders or have folders along side that can also contain folders. Commented Oct 4, 2011 at 17:52

2 Answers 2

1

Just take the arrays returned by preg_split() and build your tree structure out of them:

$tree = array();
foreach ( $strings as $string ) {
    $path = preg_split( '/(?<=[\/\\\])(?![\/\\\])/', $string );
    $ptr =& $tree;
    foreach ( $path as $elem ) {
        if ( ! array_key_exists( $elem, $ptr ) )
            $ptr[ $elem ] = array();
        $ptr =& $ptr[ $elem ];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ironically just came up with a very similar solution using the referenced tree myself. Although wasn't using array_key_exists so you've improved it a bit :)
0

You're probably best off just using pathinfo()

http://uk.php.net/manual/en/function.pathinfo.php

And realpath() http://uk.php.net/manual/en/function.realpath.php

I assume you're trying to map a *nix directory to a Windows one?

1 Comment

No the *nix example was simply to justify the forward slash part of the regex and the importance of preserving an initial /

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.