Jump to content

Converting Values Inside An Array Into A String


pioneerx01

Recommended Posts

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 by pioneerx01
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.