如何使用对象访问数组?

时间:2017-09-24 17:59:16

标签: php

我正在尝试学习如何访问数组中的对象。例如,我如何访问该名称? [name] =>每年会员资格。

Array
(
    [0] => WC_Memberships_User_Membership Object
        (
            [id] => 56325
            [plan_id] => 55119
            [plan] => WC_Memberships_Membership_Plan Object
                (
                    [id] => 55119
                    [name] => Yearly Membership
                    [slug] => yearly-membership
                    [post] => WP_Post Object
                        (
                            [ID] => 55119
                            [post_author] => 4
                            [post_date] => 2016-12-01 00:18:27
                            [post_date_gmt] => 2016-12-01 00:18:27
                            [post_content] => 
                            [post_title] => Yearly Membership
                            [post_excerpt] => 
                            [post_status] => publish
                            [comment_status] => closed
                            [ping_status] => closed
                            [post_password] => 
                            [post_name] => yearly-membership
                            [to_ping] => 
                            [pinged] => 
                            [post_modified] => 2017-09-20 01:16:15
                            [post_modified_gmt] => 2017-09-20 01:16:15
                            [post_content_filtered] => 
                            [post_parent] => 0
                            [guid] => https://sharpescout.bdanzer.com/?post_type=wc_membership_plan&p=55119
                            [menu_order] => 0
                            [post_type] => wc_membership_plan
                            [post_mime_type] => 
                            [comment_count] => 0
                            [filter] => raw
                        )

                    [access_method_meta:protected] => _access_method
                    [default_access_method:protected] => unlimited
                    [access_length_meta:protected] => _access_length
                    [access_start_date_meta:protected] => _access_start_date
                    [access_end_date_meta:protected] => _access_end_date
                    [product_ids_meta:protected] => _product_ids
                    [members_area_meta:protected] => _members_area_sections
                    [email_content_meta:protected] => _email_content
                    [rules:WC_Memberships_Membership_Plan:private] => Array
                        (
                        )

                )

            [user_id] => 317
            [status] => wcm-active
            [post] => WP_Post Object
                (
                    [ID] => 56325
                    [post_author] => 317
                    [post_date] => 2017-09-19 04:21:38
                    [post_date_gmt] => 2017-09-19 04:21:38
                    [post_content] => 
                    [post_title] => Auto Draft
                    [post_excerpt] => 
                    [post_status] => wcm-active
                    [comment_status] => closed
                    [ping_status] => closed
                    [post_password] => um_59c09b4d1c87e
                    [post_name] => auto-draft-9
                    [to_ping] => 
                    [pinged] => 
                    [post_modified] => 2017-09-19 04:21:38
                    [post_modified_gmt] => 2017-09-19 04:21:38
                    [post_content_filtered] => 
                    [post_parent] => 55119
                    [guid] => https://sharpescout.bdanzer.com/?post_type=wc_user_membership&p=56325
                    [menu_order] => 0
                    [post_type] => wc_user_membership
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

            [product:WC_Memberships_User_Membership:private] => 
            [type:protected] => manually-assigned
            [start_date_meta:protected] => _start_date
            [end_date_meta:protected] => _end_date
            [cancelled_date_meta:protected] => _cancelled_date
            [paused_date_meta:protected] => _paused_date
            [paused_intervals_meta:protected] => _paused_intervals
            [product_id_meta:protected] => _product_id
            [order_id_meta:protected] => _order_id
            [previous_owners_meta:protected] => _previous_owners
            [renewal_login_token_meta] => _renewal_login_token
        )

)

我正在使用的函数叫做:wc_memberships_get_user_active_memberships();我在我的代码中设置了:

$get_user_membership = wc_memberships_get_user_active_memberships(); 

我试图回复甚至只是id

echo $get_user_membership[0]->[id];

但我收到错误,所以我知道我做错了什么。

如何在此处访问这些值中的每一个?任何帮助都会非常有帮助

编辑: 这是我在尝试获取id的例子中得到的错误

Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in (removed filename) on line 325

2 个答案:

答案 0 :(得分:0)

您可以通过遍历数组[]和对象{}来执行此操作:

var_dump($get_user_membership[0]->{'plan'}->{'name'});

答案 1 :(得分:0)

我会说:

var_dump($get_user_membership[0]->plan->name);