Jump to content

Dynamically adding dimensions to an array using for loop


Tanner

Recommended Posts

Here is my dilemma and thank you in advance!

 

I am trying to create a variable variable or something of the sort for a dynamic associative array and having a hell of a time figuring out how to do this. I am creating a file explorer so I am using the directories as the keys in the array.

 

Example:

I need to get this so I can assign it values

 

$dir_list['root']['folder1']['folder2'] = value;

 

so I was thinking of doing something along these lines...

 

if ( $handle2 = @opendir( $theDir.'/'.$file ))
{
    $tmp_dir_url = explode($theDir);
    for ( $k = 1; $k < sizeof ( $tmp_dir_url ); $k++ )
    {
        $dir_list [ $dir_array [ sizeof ( $dir_array ) - 1 ] ][$tmp_dir_url[$k]]
    }

 

this is where I get stuck, I need to dynamically append a new dimension to the array durring each iteration through the for loop...but i have NO CLUE how

Link to comment
Share on other sites

Recursion or maybe array references or both, but this works with just recursion:

 

function dir_tree($dir) {
$files  = glob("$dir/*");

foreach($files as $file) {
	if(is_dir($file)) {
		$result[basename($file)] = dir_tree($file);
	} else {
		$result[] = basename($file);
	}
}
return $result;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.