0

Below is the array ($answers), you can see the $id in the last array item:

    14 => array (
    "Joghurtos zabkása",
    "Joghurt zabbal",
    "Joghurt zabpehellyel",
    "Reggeli zabbal",
    "Egyéb <input type=\"text\" name=\"poll".$id."\" style=\"width:100px; \" value=\"\" /> "

Inside the function:

function NewPoll($id,$type,$optional=false){
global $answers;
            foreach ( $answers[$id] as $key => $value ) {
                echo "
                <input type=\"radio\" name=\"poll".$id."\" id=\"poll".$id."[".$key."]\" value=\"".$key."\" />
            <label for=\"poll".$id."[".$key."]\">".$value."</label><br />";
            }

What I want to do is, when I am printing from the function, through the $value variable, the $id, which is from the array, should be get the $id in the function. The $id gets value in the function, and not definied outside it, but I want to use it, when it is loaded into. I don't know how clear am I...

1
  • Probably not the best way of going about this, but you could probably use a regex to grab the value of $id from the string in the array Commented Aug 9, 2013 at 16:30

3 Answers 3

1

You cannot do this. When that 14 element in the array was defined. the $id variable was replaced with its value and embedded into the string. Once you start accessing that array latter, there is NO "history" attached to that string that this particular value came from a variable, it's just a string now.

e.g. if you have

$foo = 'bar';
$baz = "This string contains $foo";
echo $baz; // prints: This string contains bar
$foo = 'qux';
echo $baz; // prints: This string contains bar

Changing $foo after the string was built will NOT change bar to qux inside that string, because the fact that bar came from a variable is lost when the string was built.

Sign up to request clarification or add additional context in comments.

3 Comments

OK, this is clear now. Is there any workaround, even if it is completely different?
Use a pseudo-templating system, e.g. instead of embedding $id directly, put in some wonky non-standard string, e.g. @@id@@ and do a string replacement on it when you wan to output the string. Or just build the string later on, instead of ahead of time.
Yeah, actually I tried that before. I have used <!-- !>. Then it seemed the first time it replaces that (the first loop), stays that way, and the later loops can not rewrite it. So this is, why I tried variables, maybe I made something wrong?
0

You cannot do this because you have already injected $id into the string.

Try var_dump($answers);, it won't show you the output which you have posted in the question (the identifier '$id' is replaced by its contents).

Comments

0

Try:

class Container {

    public static $answers = array('foo', 'bar');
}


function NewPoll($id,$type,$optional=false)
{

    foreach ( Container::$answers[$id] as $key => $value )
    //...

However, I recommend a better use of objects and classes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.