从 PHP 数组访问特定元字段

Accessing specific meta fields from a PHP array

我正在使用以下内容为当前登录的用户输出元值。

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

输出

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

效果很好,但我只需要访问数组 product_id 中的特定元字段。

我试图通过以下方式输出它,但无法让它工作:

<?php
$all_meta_for_user['product_id'][0];

你想错了。

$all_meta_for_user[0]['product_id'];

您访问数组的方式有误。请尝试以下操作:

$all_meta_for_user[0]['product_id'];