I am having some trouble accessing a specific section of this Stripe API array. I am still learning to work with this kind of pattern and the API does get updated regularly, however this particular ID is within array which I somehow cannot figure out how to access, here is what I have so far...
$subscriptions = \Stripe\Subscription::all(array('limit'=>100));
foreach($subscriptions->data as $subscription){
$sub_id = $subscription->id;
$sub_name = $subscription->plan->nickname;
$sub_item_id = $subscription->items;
print_r($sub_item_id);
}
This script outputs this:
Stripe\Collection Object ( [object] => list [data] => Array ( [0] => Stripe\SubscriptionItem Object ( [id] => si_DTLvqerPBwHgZ0 [object] => subscription_item [created] => 1535055364 [metadata] => Stripe\StripeObject Object ( ) [plan] => Stripe\Plan Object ( [id] => hithost [object] => plan [active] => 1 [aggregate_usage] => sum [amount] => [billing_scheme] => tiered [created] => 1535054527 [currency] => gbp [interval] => month [interval_count] => 1 [livemode] => 1 [metadata] => Stripe\StripeObject Object ( ) [nickname] => HitHost [product] => prod_DTLdxcoAqBC6HS [tiers] => Array ( [0] => Stripe\StripeObject Object ( [amount] => 300 [up_to] => 1 ) [1] => Stripe\StripeObject Object ( [amount] => 2 [up_to] => ) ) [tiers_mode] => graduated [transform_usage] => [trial_period_days] => [usage_type] => metered ) [subscription] => sub_DTLv0tZNFaQSEf ) ) [has_more] => [total_count] => 1 [url] => /v1/subscription_items?subscription=sub_DTLv0tZNFaQSEf )
What I am trying to access is the [id] => si_DTLvqerPBwHgZ0
I have tried the following but am either getting errors or blank screens.
$sub_item_id = $subscription->items->list;
$sub_item_id = $subscription->items->object->list;
$sub_item_id = $subscription->items->object->list[data];
Could somebody please point me in the right direction here?
I think what you want is
$sub_item_id = $subscription->items->data[0]->id
It can be easier to debug these issues if you use var_export
instead of print_r
, as it creates valid PHP code for the variable which can then be used on sites like phptester.net.