rockinaway Posted March 12, 2007 Share Posted March 12, 2007 If I have a foreach like: foreach($array as $key => $value) And I want to make a variable like: $var_$key = ..... I want the $key yo be replaced with the key from the array, what do I do to make it work? Quote Link to comment https://forums.phpfreaks.com/topic/42387-foreach-use-a-variable-in-variable-name/ Share on other sites More sharing options...
papaface Posted March 12, 2007 Share Posted March 12, 2007 Look into variable variables http://uk3.php.net/variables.variable Quote Link to comment https://forums.phpfreaks.com/topic/42387-foreach-use-a-variable-in-variable-name/#findComment-205607 Share on other sites More sharing options...
rockinaway Posted March 12, 2007 Author Share Posted March 12, 2007 I read it but didn't quite understand it ??? Quote Link to comment https://forums.phpfreaks.com/topic/42387-foreach-use-a-variable-in-variable-name/#findComment-205611 Share on other sites More sharing options...
rockinaway Posted March 12, 2007 Author Share Posted March 12, 2007 Sorry couldn't edit, but how would I do it for what I want doing, which is shown in first post??? Quote Link to comment https://forums.phpfreaks.com/topic/42387-foreach-use-a-variable-in-variable-name/#findComment-205631 Share on other sites More sharing options...
wildteen88 Posted March 12, 2007 Share Posted March 12, 2007 Do this: ${'var_' . $key} = $value; So what that will do is create a variable called var_ followed by the value $key. So if key was set to 'foobar' it would create a variable called $var_foobar Quote Link to comment https://forums.phpfreaks.com/topic/42387-foreach-use-a-variable-in-variable-name/#findComment-205643 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2007 Share Posted March 12, 2007 Why do you want to use a variable instead of the array reference? Ken Quote Link to comment https://forums.phpfreaks.com/topic/42387-foreach-use-a-variable-in-variable-name/#findComment-205700 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.