从对象数组中获取值

时间:2017-05-14 10:45:14

标签: php arrays object woocommerce

我需要在我为WooCommerce会员资格创建的自定义函数中使用数组中的值。

我正在使用代码:

//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);

这将返回所有对象的所有细节

WC_Memberships_User_Membership Object([id] => 148180 [plan_id] => 148169 [plan] => WC_Memberships_Membership_Plan对象([id] => 148169 [name] =>伦敦[slug] =>伦敦[post] => WP_Post对象([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] =>伦敦[post_excerpt] => [post_status] =>发布[comment_status] =>关闭[ping_status] =>关闭[post_password] =&gt ; [post_name] =>伦敦[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对象([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] =>打开[ping_status] =>关闭[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] =>手动分配[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)

我只是希望能够获得" id"从第一个(WC_Memberships_User_Membership Object)对象(在本例中为148180)中计算,因为我想在我的函数中使用它 - 但我不能这样做。

我最近得到的是使用此代码:

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

    echo $ids[0];

但它正在返回身份证号码' 148169'这是第二个对象。任何想法都非常感激。

1 个答案:

答案 0 :(得分:1)

<?php echo $user_membership->id; exit; ?>

试试这个