0

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 :)

8
  • Read this 3 times, but still not sure what you mean: I would like to have a loop that run over the folder names and save each file names array into a variable that has the same name as the folder name array value. -- could you explain? Commented Dec 29, 2013 at 21:31
  • How about extract() function? Commented Dec 29, 2013 at 21:32
  • @revo extract is used for associative arrays, it would not help the OP. Commented Dec 29, 2013 at 21:42
  • @Jessica He can use the same name as its value and key. Commented Dec 29, 2013 at 21:47
  • Which would still not solve the problem. If he had an array ['foo'=>'foo'] and used extract, he would then have $foo which held 'foo'. Commented Dec 29, 2013 at 21:48

1 Answer 1

3
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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.