bdmovies Posted July 24, 2009 Share Posted July 24, 2009 $info is an array: <?php foreach($info as $key => $value) { $key = $value; echo "$key = ".$value . "<br/>"; } ?> This prints VALUE = VALUE, not KEY = VALUE. Why wouldn't the key get stored as the variable name? This particular array comes from a form with nearly several inputs, so I wanted to streamline the variable naming process by using the loop, what am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/167325-solved-array-to-variables/ Share on other sites More sharing options...
akitchin Posted July 24, 2009 Share Posted July 24, 2009 that's because you're just assigning the value of $value to the variable $key. if you want to use $key as the variable name, you need to use variable variable notation: $$key = $value; however, the extract() function will do this for you. Link to comment https://forums.phpfreaks.com/topic/167325-solved-array-to-variables/#findComment-882304 Share on other sites More sharing options...
bdmovies Posted July 25, 2009 Author Share Posted July 25, 2009 Thanks very much, I knew I had seen a function that could do it, just couldn't remember. And I figured I was missing something simple, like the extra $. Thanks again. Link to comment https://forums.phpfreaks.com/topic/167325-solved-array-to-variables/#findComment-882421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.