ranslobo
ranslobo

Reputation: 57

Create variables from array values

I wrote 2 functions, one that gets all folder names and saves them in an array, and a second that gets all images file names and also saves them in array.

I would like to have a loop that runs over the folder names and saves each file name array into a variable that has the same name as the folder name array value.

It is possible?

foreach ($folders as $folder) {

    *$foldername* = getImages($folder);
}

Hopefully its even possible.

UPDATE:

I'll try to explain what I want to achive...

I have an array of my folders

$allDirs = array
(
   'cat1' => 'category1'
   'cat2' => 'category2'
   'cat3' => 'category3'
)

And I would like to run loop that create an arrays for each of values and save the variable as...

$category1= array
(
   'img1' => 'image1.jpg'
   'img2' => 'image2.jpg'
)

$category2= array
(
   'img1' => 'image1.jpg'
   'img2' => 'image2.jpg'
)

$category3= array
(
   'img1' => 'image1.jpg'
   'img2' => 'image2.jpg'
)

Hopefully this is much readable :)

Upvotes: 0

Views: 82

Answers (1)

Jessica
Jessica

Reputation: 7005

foreach ($folders as $folder) {
   $$folder = getImages($folder);
}

It's called variable variables.

Probably not a good idea, it's hard to tell from your question, but that's how you do it.

Upvotes: 3

Related Questions