Tanner Posted August 26, 2010 Share Posted August 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/211772-dynamically-adding-dimensions-to-an-array-using-for-loop/ Share on other sites More sharing options...
PravinS Posted August 26, 2010 Share Posted August 26, 2010 You have used explode() function, it will require delimiter character, as first parameter in function. Quote Link to comment https://forums.phpfreaks.com/topic/211772-dynamically-adding-dimensions-to-an-array-using-for-loop/#findComment-1103880 Share on other sites More sharing options...
Tanner Posted August 26, 2010 Author Share Posted August 26, 2010 Yeah I know, that was a typo... still isn't going to work...working on something right now though. crossing fingers Quote Link to comment https://forums.phpfreaks.com/topic/211772-dynamically-adding-dimensions-to-an-array-using-for-loop/#findComment-1103884 Share on other sites More sharing options...
AbraCadaver Posted August 26, 2010 Share Posted August 26, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/211772-dynamically-adding-dimensions-to-an-array-using-for-loop/#findComment-1104023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.