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? 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 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 ??? 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??? 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 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 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
Archived
This topic is now archived and is closed to further replies.