访问阵列由PHP函数返回 [英] Access array returned by a function in php

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

问题描述

我使用的插入code在我的网站,我想它的模板引擎。

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)'; }

问题是,这 - $>数据是私有的,我不能随时随地访问它,所以我必须使用的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]

参考:<一href=\"http://php.net/manual/en/language.types.array.php#example-102\">http://php.net/manual/en/language.types.array.php#example-102

在此之前PHP 5.3,你需要使用一个临时变量。

Prior to PHP 5.3, you'll need to use a temporary variable.

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

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