'access to value of array php [duplicate]

Array code:

        $membership = wc_memberships_get_user_active_memberships(get_current_user_id());
        $membership = $membership[0];
        print("<pre>".print_r($membership,true)."</pre>");

Small part of output:

WC_Memberships_User_Membership Object
(
    [id] => 784
    [plan_id] => 446
    [plan] => WC_Memberships_Membership_Plan Object
        (
            [id] => 446
            [name] => Test
            [slug] => membership
            [post] => WP_Post Object

I want to get [plan_id]. I tried

$membership = get_object_vars($obj);
echo $obj->plan_id;

and

echo $membership[plan_id]

But it makes critical php error. What I've done wrong?



Solution 1:[1]

I got this:

foreach ($memberships as $membership => $val) {
    echo $val->plan->id . '<br>';
};

Hope it will be helpful for someone

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stack