I tried to access with $this->$arrDataName[$key]
on the element with the key $key
from the array $this->$arrDataName
. But PHP interpretes that wrong.
我尝试使用$ this - > $ arrDataName中的键$ key在元素*问$ this - > $ arrDataName [$ key]。但PHP解释错误。
I tried it with { }
around the $arrDataName
to $this->{$arrDataName}[$key]
, but it doesn't work.
我在$ arrDataName周围尝试使用{}到$ this - > {$ arrDataName} [$ key],但它不起作用。
On php.net I found an advice, but I can't realize it.
在php.net上我找到了一个建议,但我无法实现。
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
为了将变量变量与数组一起使用,您必须解决模糊问题。也就是说,如果你写$$ a [1],那么解析器需要知道你是否想要使用$ a [1]作为变量,或者你想要将$$作为变量,然后是[1]索引来自那个变量。解决这种歧义的语法是:第一种情况为$ {$ a [1]},第二种情况为$ {$ a} [1]。
Perhaps anyone can help me.
也许任何人都可以帮助我。
Thanks!
EDIT:
I think it doesn't work, but I forgot to fill the array.
Finally it works. :)
This is the solution: $this->{$arrDataName}[$key]
我认为它不起作用,但我忘了填充阵列。最后它有效。 :)这是解决方案:$ this - > {$ arrDataName} [$ key]
4 个解决方案
#1
5
Your syntax is correct:
你的语法是正确的:
$this->{$varName}[$key]
You can also use an extra variable for this:
您还可以使用额外的变量:
$myTempArr = $this->$arrDataName;
$myTempArr[ $key ];
IMHO, readability is better that way...
恕我直言,可读性更好......
#2
2
<?php
class Foo {
public function __construct() {
$this->myArray = array('FooBar');
$arrayName = 'myArray';
echo $this->{$arrayName}[0];
}
}
new Foo;
This worked perfectly for me, it printed FooBar
.
这对我来说很完美,它印有FooBar。
#3
0
Let's assume your array is $this->arrDataName
. You have a $key
, so your object would be $this->arrDataName[$key]
.
我们假设你的数组是$ this-> arrDataName。你有一个$ key,所以你的对象是$ this-> arrDataName [$ key]。
If you want the contents of the variable which name is stored in $this->arrDataName[$key]
you should do this:
如果你想要名称存储在$ this-> arrDataName [$ key]中的变量的内容,你应该这样做:
<?php
echo ${$this->arrDataName[$key]};
?>
#4
0
Well, as far as I know, it works. Here how I tested it:
嗯,据我所知,它的确有效。我在这里测试它:
<?php
class tis
{
var $a = array('a', 'b', 'c');
var $b = array('x', 'y', 'z');
public function read($var)
{
echo $this->{$var}[1].'<br />';
}
}
$t = new tis();
$t->read('a');
$t->read('b');
?>
And the output:
并输出:
b
y
Check correctness of $arrDataName
. Turn on debuging and displaying PHP erros (including notices). Maybe you're trying to read non-existing property?
检查$ arrDataName的正确性。打开debuging并显示PHP错误(包括通知)。也许你正在尝试阅读不存在的财产?
Also, which PHP version you use? I assume PHP5?
另外,您使用的是哪个PHP版本?我假设PHP5?
#1
5
Your syntax is correct:
你的语法是正确的:
$this->{$varName}[$key]
You can also use an extra variable for this:
您还可以使用额外的变量:
$myTempArr = $this->$arrDataName;
$myTempArr[ $key ];
IMHO, readability is better that way...
恕我直言,可读性更好......
#2
2
<?php
class Foo {
public function __construct() {
$this->myArray = array('FooBar');
$arrayName = 'myArray';
echo $this->{$arrayName}[0];
}
}
new Foo;
This worked perfectly for me, it printed FooBar
.
这对我来说很完美,它印有FooBar。
#3
0
Let's assume your array is $this->arrDataName
. You have a $key
, so your object would be $this->arrDataName[$key]
.
我们假设你的数组是$ this-> arrDataName。你有一个$ key,所以你的对象是$ this-> arrDataName [$ key]。
If you want the contents of the variable which name is stored in $this->arrDataName[$key]
you should do this:
如果你想要名称存储在$ this-> arrDataName [$ key]中的变量的内容,你应该这样做:
<?php
echo ${$this->arrDataName[$key]};
?>
#4
0
Well, as far as I know, it works. Here how I tested it:
嗯,据我所知,它的确有效。我在这里测试它:
<?php
class tis
{
var $a = array('a', 'b', 'c');
var $b = array('x', 'y', 'z');
public function read($var)
{
echo $this->{$var}[1].'<br />';
}
}
$t = new tis();
$t->read('a');
$t->read('b');
?>
And the output:
并输出:
b
y
Check correctness of $arrDataName
. Turn on debuging and displaying PHP erros (including notices). Maybe you're trying to read non-existing property?
检查$ arrDataName的正确性。打开debuging并显示PHP错误(包括通知)。也许你正在尝试阅读不存在的财产?
Also, which PHP version you use? I assume PHP5?
另外,您使用的是哪个PHP版本?我假设PHP5?