Author Topic: PHP + Arrays  (Read 3193 times)

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
PHP + Arrays
« on: April 21, 2006, 08:53:56 PM »
Hi,

Ive been trying to modify my php slideshow so that in can handle multiple directories.

I read in the directory names and want to create arrays for them with the same name, but I cant figure out how to do it.

I store the dir names in an array, and loop through the array to store the dir name in a variable.

$array_name = $dir_array[$i];

$array_name = array();

I try to create an array with the name of the array being $array_name, but $array_name has the value Array.

I cant seem to figure out how to accomplish this.

Any help would be greatly appreciated.

Thanks

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP + Arrays
« Reply #1 on: April 21, 2006, 08:59:50 PM »
I'm confused by what you're trying to accomplish (after reading that 3 times).  Maybe I'm just tired.

But if you're running those 2 lines of code after each other, the second one is going to reinitialize the array back to an empty array.  I assume you know this, but obviously I'm missing something here.

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: PHP + Arrays
« Reply #2 on: April 21, 2006, 09:14:01 PM »

$dir_names 
= array(
  
'dir1',
  
'dir2',
  
'dir3',
  
// ...
);

$array_name = array();
foreach(
$dir_names AS $dir_name)
  
$array_name[$dir_name] = array();


I think at least thats what you are talking about.  Then you could do something like

$array_name
['dir1'][] = 'ober.xxx';


Or whatever to load the actual files into that directory.  If you then wanted to output all the files:

foreach($array_name AS $dir => $files)
  foreach(
$files AS $file)
    echo 
$dir'/'$file;

Canuck

  • Eh?!!
  • Founders
  • Posts: 792
  • Karma: +51/-3
  • Andy Moog Fan
    • My Website
Re: PHP + Arrays
« Reply #3 on: April 21, 2006, 09:17:41 PM »
Let me try to explain it again.

I want to read the names of the directories in the current directory and stores those names into an array, which I do (in $dir_array[]).  So that array would hold the names of the directories.

I now want to loop through $dir_array[] and create arrays with the names of those directories.

So I would want an array for every directory, the array having the same name of the directory.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: PHP + Arrays
« Reply #4 on: April 22, 2006, 12:45:53 PM »
Then I think you want to use variable variables:

$array_name = $dir_array[$i];

$$array_name = array();

http://us2.php.net/manual/en/language.variables.variable.php