用函数访问PHP数组元素? [英] Access PHP array element with a function?
本文介绍了用函数访问PHP数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用PHP的内部数组指针沿着多维数组迭代程序。我需要从当前行得到一个元素,我一直在做它像这样:
I'm working on a program that uses PHP's internal array pointers to iterate along a multidimensional array. I need to get an element from the current row, and I've been doing it like so:
$arr[key($arr)]['item']
不过,我更preFER使用类似:
However, I'd much prefer to use something like:
current($arr)['item'] // invalid syntax
我希望有一个功能,在那里,我在这将使我能够访问该元素,像这样的文档扫描已经错过了:
I'm hoping there's a function out there that I've missed in my scan of the documentation that would enable me to access the element like so:
getvalue(current($arr), 'item')
或
current($arr)->getvalue('item')
有什么建议?
推荐答案
我很怀疑有这样的功能,但它的琐碎写
I very much doubt there is such a function, but it's trivial to write
function getvalue($array, $key)
{
return $array[$key];
}
编辑:自PHP 5.4,你可以直接从功能前pressions指数数组元素,电流($ ARR)['项目']
这篇关于用函数访问PHP数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文