PHP:即时访问数组值 [英] PHP: Access Array Value on the Fly
本文介绍了PHP:即时访问数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 php 中,我经常需要使用数组来映射变量......但我似乎无法在一个班轮中做到这一点.比照例子:
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屋!
查看全文