echo64 Posted January 9, 2010 Share Posted January 9, 2010 Hello Sorry if this is a really simple question but I have run out of ideas. Is there a really simple way of doing the following: $animal_dog = $something_something[$i]['animal_dog']; $animal_cat = $something_something[$i]['animal_cat']; $animal_cow = $something_something[$i]['animal_cow']; etc, etc, etc Is there a way to simplify the process above so that I could just do: $animal_* = $something_something[$i]['animal_*']; I hope this makes sense and thanks in advance... this would be so handy to know how to do! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/ Share on other sites More sharing options...
wildteen88 Posted January 9, 2010 Share Posted January 9, 2010 Your could use extract extract($something_something[$i]); echo $animal_dog; echo $animal_cat; echo $animal_cow; Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991889 Share on other sites More sharing options...
teamatomic Posted January 9, 2010 Share Posted January 9, 2010 Close. You're just a bit backwards. <?php $dog='arf'; $cat = 'meow'; $cow = 'moo'; $animal_sounds['dog']=$dog; $animal_sounds['cat']=$cat; $animal_sounds['cow']=$cow; echo $animal_sounds['dog']."<br>";//arf echo $animal_sounds['cat']."<br>";//meow echo $animal_sounds['cow']."<p>";//moo foreach($animal_sounds as $key => $value) { echo "A $key says $value<br>"; } ?> HTH Tteamatomic oops....edited for a typo... $animal_sound = $animal_sounds Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991893 Share on other sites More sharing options...
echo64 Posted January 9, 2010 Author Share Posted January 9, 2010 Hey Thanks for the replies but that section of code (although nothing to do with animals I might add!) is from a much larger script and it needs to stay in the current format. My main issue is having to manually replicate the variables, which is why I was wondering if it's possible to use some kind of wild-card feature within PHP. If something like: "$animal_* = $something_something[$i]['animal_*'];" is possible it would save me hours of work. Perhaps this is not even possible? So just to reiterate - there could be hundreds of animals and I would have to write hundreds of lines to do what I want, but if there was something like my suggestion that actually worked I could do it all in a few lines. Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991900 Share on other sites More sharing options...
wildteen88 Posted January 9, 2010 Share Posted January 9, 2010 Thats why I suggested extract Your could use extract extract($something_something[$i], EXTR_REFS); echo $animal_dog; echo $animal_cat; echo $animal_cow; EDIT: Minor change Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991904 Share on other sites More sharing options...
oni-kun Posted January 9, 2010 Share Posted January 9, 2010 Yep. extract will create variables out of the array itself, it's what you're looking to do. Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991907 Share on other sites More sharing options...
echo64 Posted January 9, 2010 Author Share Posted January 9, 2010 ah ok! Thanks, I'll go have a play with the extract function then! Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991910 Share on other sites More sharing options...
Daniel0 Posted January 9, 2010 Share Posted January 9, 2010 I suspect there is something you could do better. Why would you want to define hundreds of variables? Why do you even want to redefine a lot of variables? Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991911 Share on other sites More sharing options...
echo64 Posted January 9, 2010 Author Share Posted January 9, 2010 Simply because it's the only way I knew how to do what I wanted... Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991913 Share on other sites More sharing options...
Daniel0 Posted January 9, 2010 Share Posted January 9, 2010 Then tell us what you need to do and we'll help you improve it. If you need to do something with all the "animals" you can use a loop. Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991915 Share on other sites More sharing options...
laffin Posted January 9, 2010 Share Posted January 9, 2010 Its better known as variable variables <?php $animals=array('dog','cat','mouse'); foreach($animals as $val) { $new_var="animal_{$val}"; $$new_var=$new_var; } var_dump($animal_dog,$animal_cat,$animal_mouse); ?> Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991918 Share on other sites More sharing options...
echo64 Posted January 9, 2010 Author Share Posted January 9, 2010 That's exactly what I was after! Thank you "laffin" and the others for their ridiculously fast help. Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991920 Share on other sites More sharing options...
teamatomic Posted January 9, 2010 Share Posted January 9, 2010 A short answer to your Q about wild cards. No. You will still have to build the array. As I showed you how to build the array extract would give you: echo "$dog //arf"; echo "$cat"; //meow echo "$cow";//moo The easiest way would be: $array = ( 'dog' => 'arf', 'cat' => 'meow', 'cow' => 'moo' ); put this in a file and include it, then you can: extract($array); or foreach($array as $key => $value) or $dog = $array['dog']; or anything else you can do with arrays HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187867-variable-variable/#findComment-991922 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.