pioneerx01 Posted November 10, 2012 Share Posted November 10, 2012 (edited) I am trying to get my values inside the array to be used in foreach function both as a text value and as a string. How to I make them do both? $variables = array("first_name", "last_name"); foreach ($variables as $variable) { $variable = passthrough($_POST['$variable']) echo "<input type='hidden' name='$variable' value='$variable'>"; } Notes: passthrough is one of my pre-difined functions the $variable should be an actual string in the name of the variable. In this case $first_name, $last_name Thanks Edited November 10, 2012 by pioneerx01 Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/ Share on other sites More sharing options...
AyKay47 Posted November 10, 2012 Share Posted November 10, 2012 Not sure on your logic here. Is list() what you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391585 Share on other sites More sharing options...
jcbones Posted November 10, 2012 Share Posted November 10, 2012 I have no Idea what the question is: Are you looking for variable variables, list(), implode(), extract(), ??? Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391605 Share on other sites More sharing options...
DavidAM Posted November 10, 2012 Share Posted November 10, 2012 $variable = passthrough($_POST['$variable']) Variables are not interpreted inside single-quoted strings. You would use double-quotes here. However, you do not need quotes there at all. Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391614 Share on other sites More sharing options...
pioneerx01 Posted November 11, 2012 Author Share Posted November 11, 2012 No, list will not do what I am looking for. I thought that I was not explaining this well enough, so let me take another stab at it. I know the logic of what I am about to explain will be a little off; but the answer you give me will help me with my "large coding picture" more efficently. Lets say that I have the following strings defined earlier in the code before the array: $a = "yellow"; $a_not = "not yellow" $b = "green"; $b_not = "not green"; Now I have this array: $myterms = array("a", "b"); Now I want to use each one of the terms in the array as a string in order to call on to the data that was defined earlier. If everything works as I would like it to, the (two) result should look like this: [1] my color of choice is yellow, but this apple is not yellow. [2] my color of choice is green, but this apple is not green. The terms in red are actually the strings that have been defined earlier and they are all related to a and b terms in the array, but used as ($a and $a_not for [1]) and ($b and $b_not for [2]) Is there a way to do something like that? Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391660 Share on other sites More sharing options...
thara Posted November 11, 2012 Share Posted November 11, 2012 why you use array? variable can directly print here... Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391661 Share on other sites More sharing options...
pioneerx01 Posted November 11, 2012 Author Share Posted November 11, 2012 why you use array? variable can directly print here... I know, but there is more going on here than what I explained. If I can get the color example working I can get my more complex code working. This is just an overly simplistic way of looking at it. The entire goal is to get the variables a and b in the string acting as $a, $a_not and &b, &b_not. Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391665 Share on other sites More sharing options...
pioneerx01 Posted November 11, 2012 Author Share Posted November 11, 2012 Ok, I have figured it out. This code seems to do exactly what is described above: $a = "yellow"; $a_not = "not yellow"; $b = "green"; $b_not = "not green"; $variables = array("a", "b"); foreach ($variables as $variable) { $not = "_not"; $not_variable = "$variable$not"; echo "The color of choice is "; print $$variable; echo ", but this apple is "; print $$not_variable; echo "<br/> "; } I feel funny using $$ but it seems to do the trick. Any potential problems that I can face using this method? Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391671 Share on other sites More sharing options...
thara Posted November 11, 2012 Share Posted November 11, 2012 Try with explode(). It will help you to convert array values into string.. Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391672 Share on other sites More sharing options...
Barand Posted November 11, 2012 Share Posted November 11, 2012 $arr = array ( 'a' => 'yellow', 'a_not' => 'not yellow', 'b' => 'green', 'b_not' => 'not green' ); $variables = array('a', 'b'); foreach ($variables as $v) { $not_v = $v.'_not'; echo "The color of choice is $arr[$v] but this apple is $arr[$not_v]<br/> "; } Quote Link to comment https://forums.phpfreaks.com/topic/270546-converting-values-inside-an-array-into-a-string/#findComment-1391678 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.