在php中,我经常需要使用数组来映射变量……但我似乎不能在单行中做到这一点。c.f.示例:
// 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];
这是一个小问题,但它每隔一段时间就会出错……我不喜欢这样的事实,我使用一个变量没有任何意义;)
发布于 2008-08-16 12:55:30
我不会为这个额外的变量而烦恼,真的。不过,如果您愿意,也可以在使用它之后将其从内存中删除:
$variable = array('a','b','c');
echo $variable[$key];
unset($variable);
或者,您可以编写一个小函数:
function indexonce(&$ar, $index) {
return $ar[$index];
}
并使用以下命令调用此函数:
$something = indexonce(array('a', 'b', 'c'), 2);
数组现在应该会自动销毁。
发布于 2011-12-23 15:36:15
这可能没有直接关系..但我在这篇文章中找到了这个特定问题的解决方案。
我从以下形式的函数中获得了一个结果。
Array
(
[School] => Array
(
[parent_id] => 9ce8e78a-f4cc-ff64-8de0-4d9c1819a56a
)
)
我需要的是parent_id值"9ce8e78a-f4cc-ff64-8de0-4d9c1819a56a“。我像这样使用了这个函数并得到了它。
array_pop( array_pop( the_function_which_returned_the_above_array() ) )
所以,它只完成了一行:)希望它能对某些人有所帮助。
发布于 2011-02-18 18:40:42
function doSomething()
{
return $somearray;
}
echo doSomething()->get(1)->getOtherPropertyIfThisIsAnObject();
https://stackoverflow.com/questions/13109
复制相似问题