PHP:上飞访问数组值 [英] PHP: Access Array Value on the Fly
本文介绍了PHP:上飞访问数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,我经常需要映射使用数组变量...但我不能似乎是能够做到这一点的一个衬垫。 C.F.例如:
In php, I often need to map a variable using an array ... but I can not seem to be able to do this in a one liner. c.f. example:
// the following results in an error:
echo array('a','b','c')[$key];
// this works, using an unnecessary variable:
$variable = array('a','b','c');
echo $variable[$key];
这是一个小问题,但它一直在一段时间曾经缠着每一个...我不喜欢这样的事实,我使用一个变量白白;)
This is a minor problem, but it keeps bugging every once in a while ... I don't like the fact, that I use a variable for nothing ;)
推荐答案
我不会理会关于额外的变量,真的。如果你想,不过,你也可以从内存中您用过之后将其删除:
I wouldn't bother about that extra variable, really. If you want, though, you could also remove it from memory after you've used it:
$variable = array('a','b','c');
echo $variable[$key];
unset($variable);
或者,你可以写一个小功能:
Or, you could write a small function:
function indexonce(&$ar, $index) {
return $ar[$index];
}
和调用这个:
$something = indexonce(array('a', 'b', 'c'), 2);
数组应该自动销毁,现在
The array should be destroyed automatically now.
这篇关于PHP:上飞访问数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文