0

I'm iterating over a folder and formatting its contents in a certain way.

I've to form an array from this set of strings:

home--lists--country--create_a_country.jpg

home--lists--country--list_countries.jpg

profile--edit--account.jpg

profile--my_account.jpg

shop--orders--list_orders.jpg

The array needs to look like this:

<?php
array(
  'home' => array(
     'lists' => array(
       'country' => array(
          'create_a_country.jpg',
          'list_countries.jpg'
       )
     )
   ),
  'profile' => array(
     'edit' => array(
       'account.jpg'
     ),
     'my_account.jpg'
   ),
  'shop' => array(
     'orders' => array(
       'list_orders.jpg',
     )
);

The thing is, the depth of the array could be infinitely deep depending on how many '--' dividers the file name has. Here's what I've tried (assuming each string is coming from an array:

    $master_array = array();
    foreach($files as $file)
    {
        // Get the extension
        $file_bits      = explode(".", $file);
        $file_ext       = strtolower(array_pop($file_bits));
        $file_name_long = implode(".", $file_bits);

        // Divide the filename by '--'
        $file_name_bits = explode("--", $file_name_long);

        // Set the file name
        $file_name      = array_pop($file_name_bits).".".$file_ext;

        // Grab the depth and the folder name
        foreach($file_name_bits as $depth => $folder)
        {
            // Create sub-arrays as the folder structure goes down with the depth
            // If the sub-array already exists, don't recreate it
            // Place $file_name in the lowest sub-array

            // .. I'm lost
        }            
    }

Can anyone shed some light on how I might do this? All insight appreciated.

w001y

2
  • Any reason you care about the file extension? Because you code does not seem to actually use it. Commented May 8, 2013 at 5:25
  • Don't care about the file extension at all, just an example above. Commented May 8, 2013 at 5:52

1 Answer 1

2

Try this:

$files=array("home--lists--country--create_a_country.jpg","home--lists--country--list_countries.jpg","profile--edit--account.jpg","profile--my_account.jpg","shop--orders--list_orders.jpg");
$master_array=array();
foreach($files as $file)
{
    $file=explode("--",$file);
    $cache=end($file);
    while($level=prev($file))
    {
        $cache=array($level=>$cache);
    }
    $master_array=array_merge_recursive($master_array,$cache);
}
print_r($master_array);

Live demo

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

3 Comments

Great answer - sorts out what I want to do perfectly. Thanks Passerby, great help!!
Hey Passerby, I've done some playing with it and realised I need to pass the original file name (full string) in with the newly-formed file names - so that the new file name can reference the old file location. Can you think of a way to do this? For example create_a_country.jpg needs to reference home--lists--country--create_a_country.jpg .. thanks for your help so far :)
@w001y I don't quite get it. You can reference something to a variable, but you can't reference "the position of something in an array", so if you want to point target.jgp to path--to--target.jpg, all you need is a one-level array ("target.jpg"=>"path--to--target.jpg").

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.