Stack Exchange
preg_split string to multidimensional array
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() ? )

what is the need to have that weird structure?

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

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.

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 ];
    }
}


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

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?

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