blurredvision Posted June 13, 2008 Share Posted June 13, 2008 I have a FOREACH loop running on an array. With this FOREACH, each index is returned as the variable, $wildcard. What I'd like to do is that for each index that runs, I'd like to turn the value of the $wildcard variable into it's own variable. For example: include('config.php'); $wildcardarray = array ('apple', 'orange', 'banana'); foreach ($wildcardarray as $wildcard) { if ($wildcard == 'apple' || $wildcard == 'orange' || $wildcard = 'banana') { echo '<tr><td>' . $variable_in_question . ' <input type=checkbox blah blah />'; } } That's a rough example of what I'm trying to do, but where it has the $variable_in_question, I want that to be a dynamic variable that is created from whatever the value of $wildcard is. So if $wildcard == apple, I want that variable to be $apple. The $apple variable then comes from the included config file where I've stored multiple strings in a central location for easy editing. In this example, $apple would be something like this: $apple = 'Check here for apple.' Not the greatest example, but it should work to get my point across. Can this be done, and how? Thanks a lot for any help! Link to comment https://forums.phpfreaks.com/topic/110063-solved-how-to-turn-a-variable-string-into-its-own-variable/ Share on other sites More sharing options...
Perad Posted June 13, 2008 Share Posted June 13, 2008 $wildcardarray = array ('apple', 'orange', 'banana'); foreach ($wildcardarray as $key => $wildcard) { if ($wildcard == 'apple' || $wildcard == 'orange' || $wildcard = 'banana') { echo '<tr><td>' . $wildcard . ' <input type=checkbox blah blah />'; } } Link to comment https://forums.phpfreaks.com/topic/110063-solved-how-to-turn-a-variable-string-into-its-own-variable/#findComment-564779 Share on other sites More sharing options...
thebadbad Posted June 13, 2008 Share Posted June 13, 2008 Perad, not what he's after. blurredvision, you're looking for a variable variable, i.e. $$wildcard <?php include('config.php'); $wildcardarray = array ('apple', 'orange', 'banana'); foreach ($wildcardarray as $wildcard) { if ($wildcard == 'apple' || $wildcard == 'orange' || $wildcard = 'banana') { echo '<tr><td>' . $$wildcard . ' <input type=checkbox blah blah />'; } } ?> Link to comment https://forums.phpfreaks.com/topic/110063-solved-how-to-turn-a-variable-string-into-its-own-variable/#findComment-564787 Share on other sites More sharing options...
blurredvision Posted June 13, 2008 Author Share Posted June 13, 2008 Awesome, didn't realize it'd be that easy. Let me ask another question, then. Using my example, what if my array looked like this: <?php include('config.php'); $wildcardarray = array ('$apple', '$orange', '$banana'); foreach ($wildcardarray as $wildcard) { if ($wildcard == 'apple' || $wildcard == 'orange' || $wildcard = 'banana') { echo '<tr><td>' . $$wildcard . ' <input type=checkbox blah blah />'; } } ?> This example more closely mirrors the actual work I'm doing. To shed a bit more light on this, I'm creating my array with an fgets from the first line of a text file, and each index of the array is created from comma delimited list. This line of the text file would look something like this: #$item1,$item2,$item3,$item4 ...which would make the array look something like this: $wildcardarray = (explode(",",substr(fgets($read),1))); The # is to comment out that first line so our java program won't see it. The $wildcardarray then looks like ([0] => $item1, [1] => $item2, so-on and so-forth). I haven't checked with the superiors yet, but I'm guessing they'll want to keep the $ there since these are the variable names we'll be using. However, the $ isn't really needed in this text file, and if I take it out, the variable variable works great. However, how can I leave the $ in the values of the array and still get it to reference the config file and take the appropriate string? I tried something like this, but it didn't work: $(substr($wildcard,1) If that's confusing, then I apologize, I even confuse myself when I actually try to explain it. If you guys do not understand what I'm trying to do, then forget about it, and I'll push to get rid of the $ in the text file I'm creating the array from. Thanks! Link to comment https://forums.phpfreaks.com/topic/110063-solved-how-to-turn-a-variable-string-into-its-own-variable/#findComment-564799 Share on other sites More sharing options...
blurredvision Posted June 13, 2008 Author Share Posted June 13, 2008 Nevermind about my previous post, I got it working now. I just had to do $wildcard = substr($wildcard,1); ...before my echo statement, then use $$wildcard ...in my echo statement. I was just implementing it wrong. Thanks a lot for the help!!! Link to comment https://forums.phpfreaks.com/topic/110063-solved-how-to-turn-a-variable-string-into-its-own-variable/#findComment-564809 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.