从返回的对象访问值

时间:2013-07-17 14:13:25

标签: php oop

这是我返回的对象的print_r():

    Array
(
    [0] => stdClass Object
        (
            [list_id] => 547009977
            [list_name] => Master List
            [list_type] => email
            [member_data] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => work_phone
                            [value] => 
                        )

                    [1] => stdClass Object
                        (
                            [name] => city
                            [value] => 
                        )

                    [2] => stdClass Object
                        (
                            [name] => restricted_since
                            [value] => 
                        )

                    [3] => stdClass Object
                        (
                            [name] => unsub_campaign_id
                            [value] => 
                        )

                    [4] => stdClass Object
                        (
                            [name] => title
                            [value] => 
                        )

                    [5] => stdClass Object
                        (
                            [name] => comments
                            [value] => 
                        )

                    [6] => stdClass Object
                        (
                            [name] => company_name
                            [value] => 
                        )

                    [7] => stdClass Object
                        (
                            [name] => Info
                            [value] => 
                        )

                    [8] => stdClass Object
                        (
                            [name] => address_hash
                            [value] => 
                        )

                    [9] => stdClass Object
                        (
                            [name] => hash
                            [value] => 2054ee9827
                        )

                    [10] => stdClass Object
                        (
                            [name] => country
                            [value] => 
                        )

                    [11] => stdClass Object
                        (
                            [name] => id
                            [value] => 45
                        )

                    [12] => stdClass Object
                        (
                            [name] => gender
                            [value] => 
                        )

                    [13] => stdClass Object
                        (
                            [name] => postalcode
                            [value] => 
                        )

                    [14] => stdClass Object
                        (
                            [name] => address_1
                            [value] => 
                        )

                    [15] => stdClass Object
                        (
                            [name] => create_date
                            [value] => 2013-07-15T15:31:35+00:00
                        )

                    [16] => stdClass Object
                        (
                            [name] => optin_status_last_updated
                            [value] => 2013-07-15T15:31:35+00:00
                        )

                    [17] => stdClass Object
                        (
                            [name] => Purchased
                            [value] => 
                        )

                    [18] => stdClass Object
                        (
                            [name] => marital_status
                            [value] => 
                        )

                    [19] => stdClass Object
                        (
                            [name] => optin_status
                            [value] => null
                        )

                    [20] => stdClass Object
                        (
                            [name] => last_updated
                            [value] => 2013-07-15T15:31:35+00:00
                        )

                    [21] => stdClass Object
                        (
                            [name] => address_2
                            [value] => 
                        )

                    [22] => stdClass Object
                        (
                            [name] => home_phone
                            [value] => 
                        )

                    [23] => stdClass Object
                        (
                            [name] => fax
                            [value] => 
                        )

                    [24] => stdClass Object
                        (
                            [name] => first_name
                            [value] => CoregTest
                        )

                    [25] => stdClass Object
                        (
                            [name] => bounce_campaign_id
                            [value] => 
                        )

                    [26] => stdClass Object
                        (
                            [name] => Page
                            [value] => 
                        )

                    [27] => stdClass Object
                        (
                            [name] => is_cass_certified
                            [value] => 0
                        )

                    [28] => stdClass Object
                        (
                            [name] => last_name
                            [value] => 
                        )

                    [29] => stdClass Object
                        (
                            [name] => restricted
                            [value] => 0
                        )

                    [30] => stdClass Object
                        (
                            [name] => mobile_phone
                            [value] => 
                        )

                    [31] => stdClass Object
                        (
                            [name] => email_address
                            [value] => coregtest@chrisjallen.com
                        )

                    [32] => stdClass Object
                        (
                            [name] => ip_address
                            [value] => 
                        )

                    [33] => stdClass Object
                        (
                            [name] => state
                            [value] => 
                        )

                )

        )

)

我想要做的就是访问我的php代码中的list_id。如果我把它放入$ result var:

 $result = $vr->searchListMembers( array(
        'session_id'  => $sid,
        'field_name'  => 'email_address',
        'field_value' => $email,
        'max_records' => 1
    ) );

我试过

$result->list_id

$result[0]['list_id']

这看似很明显,但我在这里遗漏了一些东西。

6 个答案:

答案 0 :(得分:2)

$list_id = $result['list_id'];

但是,list_id不在您的代码中。所以也许你的意思是session_id。无论如何,语法是:

array['key']

答案 1 :(得分:2)

尝试:

$list_id = $result[0]->list_id;

答案 2 :(得分:1)

ChromePHP似乎不是一个非常有用的工具。这是因为PHP和JavaScript具有不同的数据类型/数据结构,因此JavaScript控制台输出不会告诉您对象在PHP中的外观。

在JavaScript中,可以使用[]访问对象和数组。在PHP中,使用不同的语法访问对象和数组。 PHP数组使用[],对象使用->

如果您想调试PHP,我建议您使用var_dump(或print_r),这将显示您是否有数组或对象。 JavaScript控制台总是显示对象,并且不是很有用。

在您的情况下,您需要通过以下方式访问您的元素:

$result[0]->list_id

答案 3 :(得分:0)

省略list_I'd不存在的事实,它实际上取决于您正在使用的searchListMember方法的返回类型。如果返回的var是一个你必须使用的对象 - > list_id如果它是一个数组那么它真的是关于结构但它应该像$ result ['list_id']

答案 4 :(得分:0)

试试这个!

print $result[0]->list_id;

答案 5 :(得分:0)

如果你这样做:

$result = $vr->searchListMembers( array(
        'session_id'  => $sid,
        'field_name'  => 'email_address',
        'field_value' => $email,
        'max_records' => 1
    ) );

$ result是一个数组。

首先,您必须访问第一个元素:$result[0]

然后,它是一个stdClass对象,因此您可以使用->运算符访问其属性:

$result[0]->list_id