访问函数返回的数组 [英] Access an Array Returned by a Function
本文介绍了访问函数返回的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法直接访问数组中返回的数据而无需临时变量?
Is there anyway to directly access the data returned in an array without a temporary variable?
目前,我的代码如下:
function getData($id) {
// mysql query
return mysql_fetch_array($result);
}
$data = getData($id);
echo $data['name'];
有没有没有临时变量直接获取返回数据的方法?
Is there a direct way to get the returned data without the temporary variable?
推荐答案
PHP 5.4 添加了数组解引用,这里是 PHP 的 的例子数组文档:
PHP 5.4 added array Dereferencing, here's the example from PHP's Array documentation:
示例 #7 数组解引用
function getArray() {
return array(1, 2, 3);
}
// on PHP 5.4
$secondElement = getArray()[1];
// previously
$tmp = getArray();
$secondElement = $tmp[1];
这篇关于访问函数返回的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文