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...

4

3 回答 3

1

你不能做这个。当14数组中的那个元素被定义时。该$id变量被其值替换并嵌入到字符串中。一旦您稍后开始访问该数组,就没有附加到该字符串的“历史”,即该特定值来自变量,现在它只是一个字符串。

例如,如果你有

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

$foo在构建字符串后更改不会更改bar到该字符串内部,因为来自变量qux的事实在构建字符串时丢失了。bar

于 2013-08-09T16:31:39.067 回答
0

尝试:

class Container {

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


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

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

但是,我建议更好地使用对象和类

于 2013-08-09T16:49:52.597 回答
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).

于 2013-08-09T16:29:51.107 回答