5.4 取消引用有效的 5.3 数组调用 [英] 5.4 dereferencing to valid 5.3 array call

查看:44
本文介绍了5.4 取消引用有效的 5.3 数组调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这行使用取消引用的代码中遇到错误:

I got an error on this line of code which uses dereferencing:

   $data['data'] = $results->result()[0];

(我从 PHP 5.4 开始学习 PHP.)如何以 5.3 的方式取消引用?

(I started learning PHP with PHP 5.4.) How can I dereference in a 5.3 manner?

我已经检查了文档:

function getArray() {
    return array(1, 2, 3);
}

// on PHP 5.4
$secondElement = getArray()[1];

// before PHP 5.4
$tmp = getArray();
$secondElement = $tmp[1];

// or
list(, $secondElement) = getArray();

但是创建方法调用似乎很麻烦

but creating a method call seems cumbersome

推荐答案

$res = $results->result();
$data['data'] = $res[0];

或者您可以使用重新分配(以避免需要临时变量):

Or you can use reassignment (to avoid the need for temporary variables):

$data['data'] = $results->result();
$data['data'] = $data['data'][0];

这篇关于5.4 取消引用有效的 5.3 数组调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆