dk4210 Posted December 5, 2011 Share Posted December 5, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/252506-variable-to-array/ Share on other sites More sharing options...
btellez Posted December 5, 2011 Share Posted December 5, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/252506-variable-to-array/#findComment-1294588 Share on other sites More sharing options...
SergeiSS Posted December 5, 2011 Share Posted December 5, 2011 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: <?php $images=array(); for( $i=1; $i<=10; $i++) // may be not 10 but a number of loaded images $images[]=sprintf( '2011-%02d', $i ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/252506-variable-to-array/#findComment-1294590 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.