0

I am using the following to output meta values for a current logged in user.

<?php
$user_id = get_current_user_id();
$key = 'chargebee_user_subscriptions';
$all_meta_for_user = get_user_meta( $user_id, $key, true  );
print_r($all_meta_for_user);

Output

Array ( [0] => Array ( [subscription_id] => IaQX8AtBRo [product_id] => cbdemo_grow [product_name] => Plan - Grow [product_decs] => This a 3-month plan with a 14 day trial period. [status] => in_trial [product_price] => 89 USD / 3 month [trail_start] => 08/02/2018 [trial_end] => 22/02/2018 ) )

This works great, but I only need to access a specific meta field from the array product_id.

I've attempted to output this via the following but cannot get it to work:

<?php
$all_meta_for_user['product_id'][0];
4
  • 1
    please do a little debug work before posting here (with print_r or echo), as you could have easily found the mistake in $all_meta_for_user (array indexes swapped). Commented Feb 9, 2018 at 19:03
  • Will do, apologies got it sorted now :) Commented Feb 9, 2018 at 19:04
  • @Pierre, he did do some debugging. As you can see he even posted it. (Array ( [0] => Array ( [subscription_id] => IaQX8AtBRo [product_id] => cbdemo_grow [product_name] => Plan - Grow [product_decs] => This a 3-month plan with a 14 day trial period. [status] => in_trial [product_price] => 89 USD / 3 month [trail_start] => 08/02/2018 [trial_end] => 22/02/2018 ) ) ) Commented Feb 9, 2018 at 19:10
  • This question is similar to: How can I access an array/object?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 15, 2024 at 18:05

2 Answers 2

2

You have it the wrong way around.

$all_meta_for_user[0]['product_id'];
Sign up to request clarification or add additional context in comments.

1 Comment

Doh! That makes sense. Thanks.
-1

You are accessing the array in a wrong way. Try the following:

$all_meta_for_user[0]['product_id'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.