php php中函数返回的访问数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1459377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access array returned by a function in php
提问by enyo
I'm using a template engine that inserts code in my site where I want it.
我正在使用一个模板引擎,它可以在我的站点中插入我想要的代码。
I wrote a function to test for something which is quite easy:
我写了一个函数来测试一些很容易的东西:
myfunction() { return '($this->data["a"]["b"] ? true : false)'; }
The problem is, $this->data is private, and I can't access it everywhere, so I have to use getData(); which causes my problem.
问题是,$this->data 是私有的,我不能到处访问它,所以我必须使用 getData(); 这导致了我的问题。
$this->getData()['a']['b']
does not work, and assigning the value first doesn't either because it will be used directly in an if() block.
不起作用,首先分配值也不起作用,因为它将直接在 if() 块中使用。
Any ideas?
有任何想法吗?
回答by enyo
Since PHP 5.4 it's possible to do exactly that:
从 PHP 5.4 开始,完全可以做到这一点:
getSomeArray()[2]
Reference: https://secure.php.net/manual/en/language.types.array.php#example-62
参考:https: //secure.php.net/manual/en/language.types.array.php#example-62
On PHP 5.3 or earlier, you'll need to use a temporary variable.
在 PHP 5.3 或更早版本中,您需要使用临时变量。
回答by Pascal MARTIN
You cannot use something like this :
你不能使用这样的东西:
$this->getData()['a']['b']
ie, array-access syntax is not possible directly on a function-call.
即,不能直接在函数调用上使用数组访问语法。
Youy have to use some temporary variable, like this :
你必须使用一些临时变量,像这样:
$tmp = $this->getData();
$tmp['a']['b'] // use $tmp, now
In your case, this probably means using something like this :
在你的情况下,这可能意味着使用这样的东西:
function myfunction() {
$tmp = $this->getData();
return ($tmp['a']['b'] ? true : false);
}
You have to :
你必须 :
- first, call your
getData()method, and store its return value in a temporary varibale - then, use that temporary variable for your test
- 首先,调用您的
getData()方法,并将其返回值存储在临时变量中 - 然后,使用该临时变量进行测试
You don't have much choice about that, actually...
你没有太多选择,实际上......
回答by enyo
Ok... apparently there really isn't a better way, so I'm going to answer myself with a not so beautiful solution:
好吧......显然真的没有更好的方法,所以我要用一个不太漂亮的解决方案来回答自己:
I created the function:
我创建了函数:
arrayGet($array, $index) { return $array[$index]; }
And used it like this:
并像这样使用它:
myfunction() { return '(arrayGet(arrayGet($this, "a"), "b") ? true : false)' }
This is not pretty but works.
这不漂亮,但有效。
回答by soulmerge
$this->datais alwaysaccessible, if it is protected. $object->datais not accessible from everywhere, so if you're returning $thisin your code, and it is evaluated as such, it should be ok.
$this->data是始终可以访问,如果保护。无法从任何地方访问,因此如果您返回代码并对其进行评估,则应该没问题。$object->data$this
Btw, there is a bug in your code: The quotes need to be escaped.
顺便说一句,您的代码中有一个错误:引号需要转义。
myfunction() { return '($this->data[\'a\'][\'b\'] ? true : false)'; }
回答by Lajos Meszaros
It is possible from PHP version 5.4.
可以从 PHP 5.4 版开始。
If you don't want a temporary variablefor that and your PHP version is less, than 5.4, than you still can use a few built in functions to get the first or the last element:
如果你不希望一个临时变量为你的PHP版本是小于,5.4,比你还可以使用内置函数的几个获得第一或最后一个元素:
$x = 'first?last';
$first = array_shift(explode('?', $x));
$last = end(explode('?', $x));
$last2 = array_pop(explode('?', $x));
Edit:!!! Please note, that in later versions( 5.4+ ) PHP will throw a notice, because end only expects variables as parameter.
编辑:!!!请注意,在以后的版本(5.4+)中 PHP 会抛出一个通知,因为 end 只需要变量作为参数。

