php - 访问数组 php 的 value

数组代码:

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

输出的一小部分:

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

我想得到 [plan_id]。我试过了

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

echo $membership[plan_id]

但它会导致严重的 php 错误。我做错了什么?

回答1

我懂了:

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

希望对某人有所帮助

相似文章