I need to use a value within an array in a custom function I am creating for WooCommerce memberships.

I am using the code:

//Set Arguments    
$args = array(
    'plan_id' => 148169, //This is the plan number
    'user_id' => $user_id,
    );

// Return the membership number
    $user_membership = wc_memberships_get_user_membership( $user_id, $args['plan_id'] );

// Print the Membership Number (Test Only)
    print_r($user_membership);

This returns me all of the details of all of the objects

WC_Memberships_User_Membership Object ( [id] => 148180 [plan_id] => 148169 [plan] => WC_Memberships_Membership_Plan Object ( [id] => 148169 [name] => London [slug] => london [post] => WP_Post Object ( [ID] => 148169 [post_author] => 1 [post_date] => 2017-05-14 11:12:52 [post_date_gmt] => 2017-05-14 10:12:52 [post_content] => [post_title] => London [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => london [to_ping] => [pinged] => [post_modified] => 2017-05-14 11:12:52 [post_modified_gmt] => 2017-05-14 10:12:52 [post_content_filtered] => [post_parent] => 0 [guid] => http://dev3.benefacto.org/?post_type=wc_membership_plan&p=148169 [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] => 1 [status] => wcm-active [post] => WP_Post Object ( [ID] => 148180 [post_author] => 1 [post_date] => 2017-05-14 11:31:51 [post_date_gmt] => 2017-05-14 10:31:51 [post_content] => [post_title] => [post_excerpt] => [post_status] => wcm-active [comment_status] => open [ping_status] => closed [post_password] => um_59183217123cc [post_name] => 148180 [to_ping] => [pinged] => [post_modified] => 2017-05-14 11:31:51 [post_modified_gmt] => 2017-05-14 10:31:51 [post_content_filtered] => [post_parent] => 148169 [guid] => http://dev3.benefacto.org/?post_type=wc_user_membership&p=148180 [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 )

I simply want to be able to get the “id” figure from within the first (WC_Memberships_User_Membership Object) object (in this case 148180) because I want to use it in my function – but I can’t do it.

Closest I’ve got is using this code:

$umarray = get_object_vars($user_membership);
    $ids = array_column($umarray, 'id');

    echo $ids[0];

But it is returning the ID figure ‘148169’ which is for the second object. Any thoughts much appreciated.

Read more here: Get a value from within an array of objects


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: