简体   繁体   English

如何使用 PHP 从循环中的复杂数组中获取值?

[英]How to get values from a complex array in a loop using PHP?

I'm trying to get the list of all stripe subscribers on my PHP page.我试图在我的 PHP 页面上获取所有条纹订阅者的列表。

I have managed to get the details of 1 subscriber from the array that's returned from stripe API so far.到目前为止,我已经设法从条带 API 返回的数组中获取了 1 个订阅者的详细信息。

But when I try to get the details of 2 or 3 etc subscribers, I get nothing.但是当我尝试获取 2 或 3 个等订阅者的详细信息时,我什么也没得到。

I know I will need to use a loop or for each to achieve this but I can't figure out how.我知道我需要使用循环或每个循环来实现这一点,但我不知道如何。

This is my current code to get the details of 1 subscriber:这是我当前获取 1 个订阅者详细信息的代码:

$data = \Stripe\Subscription::all(['limit' => 1]);

$sub_id = $data->data[0]->id;

echo $sub_id;

Now I need to get the details of more than 1 subscriber so I tried this which didn't work and doesn't return anything (no errors either):现在我需要获取超过 1 个订阅者的详细信息,所以我尝试了这个,但它不起作用并且不返回任何内容(也没有错误):

 $data = \Stripe\Subscription::all(['limit' => 2]);

foreach ($data as $k => $v) { 

echo $data->data[0]->id;


}

could someone please advise on this issue?有人可以就这个问题提出建议吗?

You probably need to loop $data->data as it is an array of objects and then access the id property of each object:您可能需要循环$data->data因为它是一个对象数组,然后访问每个对象的id属性:

foreach ($data->data as $v) { 
    echo $v->id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM