views:

23

answers:

2

I have array like this:

$path = array (
[0] => site\projects\terrace_and_balcony\mexico.jpg
[1] => site\projects\terrace_and_balcony\new_york.jpg
[2] => site\projects\terrace_and_balcony\berlin.jpg
[3] => site\projects\terrace_and_balcony\Kentucky.jpg
[4] => site\projects\terrace_and_balcony\Utah.jpg
[5] => site\projects\terrace_and_balcony\Hawaii.jpg
[6] => site\projects\private_gardens\mexico.jpg
[7] => site\projects\private_gardens\new_york.jpg
[8] => site\projects\private_gardens\berlin.jpg
[9] => site\projects\private_gardens\Kentucky.jpg
[10] => site\projects\private_gardens\Utah.jpg
[11] => site\projects\private_gardens\Hawaii.jpg
)

How to convert it to that:

$path11 = array
(
    "site"=>array
    (
        "projects"=>array
        (
            "terrace_and_balcony"=>array
            (
                "mexico.jpg",
                "new_york.jpg",
                "berlin.jpg",
                "Kentucky.jpg",
                "Utah.jpg",
                "Hawaii.jpg"
            ),
            "private_gardens"=>array
            (
                "mexico.jpg",
                "new_york.jpg",
                "berlin.jpg",
                "Kentucky.jpg",
                "Utah.jpg",
                "Hawaii.jpg"
            )
        )
    )
);
+1  A: 

All you need is explode() function and foreach loops.

smentek
+1  A: 
$tree = array();
foreach ($paths as $path) {
    $pathParts = explode('\\', $path);
    $subTree = array(array_pop($pathParts));
    foreach (array_reverse($pathParts) as $dir) {
        $subTree = array($dir => $subTree);
    }
    $tree = array_merge_recursive($tree, $subTree);
}
print_r($tree);
chris
Works like a charm, many thanks chris.RegardsKelNoReem
KelNoReem