php中函数返回的访问数组 [英] Access array returned by a function in php

查看:29
本文介绍了php中函数返回的访问数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个模板引擎,它可以在我的网站中插入我想要的代码.

I'm using a template engine that inserts code in my site where I want it.

我写了一个函数来测试一些很简单的东西:

I wrote a function to test for something which is quite easy:

myfunction() { return '($this->data["a"]["b"] ? true : false)'; }

问题是,$this->data是私有的,到处都无法访问,所以不得不使用getData();这导致了我的问题.

The problem is, $this->data is private, and I can't access it everywhere, so I have to use getData(); which causes my problem.

$this->getData()['a']['b']

不起作用,首先赋值也不起作用,因为它将直接在 if() 块中使用.

does not work, and assigning the value first doesn't either because it will be used directly in an if() block.

有什么想法吗?

推荐答案

自 PHP 5.4 起,完全可以做到这一点:

Since PHP 5.4 it's possible to do exactly that:

getSomeArray()[2]

参考:https://secure.php.net/manual/en/language.types.array.php#example-62

在 PHP 5.3 或更早版本中,您需要使用临时变量.

On PHP 5.3 or earlier, you'll need to use a temporary variable.

这篇关于php中函数返回的访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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