如何从对象数组中获取数据 [英] How to get data from array in object

查看:970
本文介绍了如何从对象数组中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法从对象内部的数组中获取特定数据.

I can't seem to get specific data from an array inside an object.

$this->fields->adres正确获取地址,但是我无法更深入地了解该地址.

$this->fields->adres gets the address correctly, but i can't get a level deeper.

我尝试过:

$this->fields->province
$this->fields->province->0
$this->fields->province[0]

并且:(编辑)

$this->fields["province"][0]
$this->fields['province'][0]
$this->data->fields['province'][0]

但是它不返回任何内容,而应该返回"Flevoland".

But it does not return anything while it should return "Flevoland".

下面的对象print_r($ this,TRUE)的第一部分:

First part of the object print_r($this, TRUE) below:

RSMembershipModelSubscribe Object
(
    [_id] => 2
    [_extras] => Array
        (
        )

    [_data] => stdClass Object
        (
            [username] => testzz
            [name] => testzz
            [email] => xxxx@example.com
            [fields] => Array
                (
                    [province] => Array
                        (
                            [0] => Flevoland
                        )

                    [plaats] => tesdt
                    [adres] => test

推荐答案

从输出中可以看到,对象成员可能是私有的(如果遵循约定,无论如何,在调用它们时都必须在其前面加上下划线),因此你打错电话给他们; 这段代码有效:

As you can see by your output, object members are likely to be private (if you follow conventions, anyway you must prepend an underscore while calling them), so you're calling them the wrong way; This code works:

$this->_data->fields['province'][0];

您可以在此处看到它. 我创建了一个类似的对象,并使用

You can see it in action here; I created a similar object, and using

$membership = new RSMembershipModelSubscribe();
echo $membership->_data->fields['province'][0];

按预期输出"Flevoland".

outputs "Flevoland" as expected.

这篇关于如何从对象数组中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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