PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 17, 2022

[FIXED] How to echo membership values from this array?

 January 17, 2022     arrays, php, woocommerce, woocommerce-memberships, wordpress     No comments   

Issue

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"
    

Solution

$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;

}



Answered By - Chris
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing