Jump to content

Variable to array


Recommended Posts

Hello Guys.


I have a question here..


I have a photo upload script that uploads photos to the server and then sends the photo name to another page via POST.


I only have one var name called $image_all


when I echo it this is what I get (When uploading 2 files)

This is the list 2011-12-05_0751.png,2011-12-05_0747.png


I also tested with "explode"

 print_r(explode(',', $image_all, 10));


and I get this


Array ( [0] => 2011-12-05_0751.png [1] => 2011-12-05_0747.png [2] => 


What I would like for it to do is assign an incremental var name to each image uploaded and passed in the POST.


Like this


$image1 =  2011-xx.png

$image2 = 2011-xx.png

$image3 = 2011-xx.png


It should created the variable names dynamically based on the number of images (Up to 10)


Thanks for your help!




Link to comment
Share on other sites

Here's some code, but why not just keep it in an array if I may ask...

$offset = 1;
for($count =  0; $count < count($image_all); $count++)
$varName = 'image'.($count + $offset);
${$varName} = $image_all[$count];

echo $image1;
echo $image2;

Link to comment
Share on other sites

And what is not clear? :)


Take for() loop and set variables. I think it's better to use an array but not a set of variables:




for( $i=1; $i<=10; $i++) // may be not 10 but a number of loaded images

  $images[]=sprintf( '2011-%02d', $i );


Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

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