0

I am trying to learn how to access objects in an array. For example how do I access the name? [name] => Yearly Membership.

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
        )

)

The function I am using is called: wc_memberships_get_user_active_memberships(); and I set in my code:

$get_user_membership = wc_memberships_get_user_active_memberships(); 

I tried to echo out even just the id

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

But I get an error so I know I am doing something wrong.

How can I access each of these values in this? Any help would be very helpful

edit: here is the error I got in the example of trying to get id

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

2 Answers 2

1

I would say:

var_dump($get_user_membership[0]->plan->name);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This works well too, and is a lot cleaner than the other answer given. I appreciate it. Is this the best solution? Or would there be better ways to access the data?
Please stay on topic, answers should not be used to query or question.
@bdanzer I'd use curly brackets for accessing an object's properties these ways: $obj->{$variable}; $obj->{"key_$variable"}; $obj->{'space inproperty'}. otherwise I like to keep it simple $obj->property
0

You can do it by running around array [] and object {} so:

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

4 Comments

Thanks! That did end up working for me when I did: echo $get_user_membership[0]->{'plan'}->{'name'}; . Is there other methods of accessing the same thing or is this the best solution?
$get_user_membership[0]->{'plan'}->{'name'} is just a complicated way to write $get_user_membership[0]->plan->name. It doesn't answer the question.
@axiac The question was How can I access each of these values in this? and the answerer has provided a solution therefore I'd argue that it is in fact a valid answer, whether it is done in a verbose way or not is not an issue.
@bdanzer Hi is the simple way to known how without any loop, thinking on how to get and run around object array multidimensional. Now you can implement a simple loop to the other value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.