Home > OS >  How to echo membership values from this array?
How to echo membership values from this array?

Time:11-10

Been trying to echo these individual values from this array

echo var_dump($memberships);

The two values I'm trying to echo are the ["name"] and ["status"]

This is what I've tried to echo those values:

if ( $memberships ) {
  foreach( $memberships[0] as $membership ) {
    echo $membership["plan"]["name"];
  }
}

I've also tried this:

echo $memberships[0]['plan']['name'];

This is part of the var_dump (stack won't allow me to put the whole thing):

array(1) {
  [0]=>
  object(WC_Memberships_Integration_Subscriptions_User_Membership)#23837 (22) {
    ["subscription_id":protected]=>
    int(103981)
    ["subscription_id_meta":protected]=>
    string(16) "_subscription_id"
    ["has_installment_plan_meta":protected]=>
    string(21) "_has_installment_plan"
    ["free_trial_end_date_meta":protected]=>
    string(20) "_free_trial_end_date"
    ["id"]=>
    int(104104)
    ["plan_id"]=>
    int(98065)
    ["plan"]=>
    object(WC_Memberships_Integration_Subscriptions_Membership_Plan)#23902 (19) {
      ["installment_plan_meta":protected]=>
      string(30) "_subscription_installment_plan"
      ["id"]=>
      int(98065)
      ["name"]=>
      string(19) "Full Premium Member"
    

CodePudding user response:

$memberships is an array of objects. Accessing members of an object is done via -> operator instead of curly braces:

foreach( $memberships as $membership ) {
echo $membership->plan->name;

}

  • Related