gpt4 book ai didi

php - Yelp API 输出错误

转载 作者:可可西里 更新时间:2023-11-01 13:31:46 28 4
gpt4 key购买 nike

我正在尝试使用 Yelp API2 做一些事情

$response = json_decode($data);

它以 PHP 对象格式返回数据,如下所示:

stdClass Object
(
[region] => stdClass Object
(
[span] => stdClass Object
(
[latitude_delta] => 0.28083237848028
[longitude_delta] => 0.23501544732261
)

[center] => stdClass Object
(
[latitude] => 31.335313781127
[longitude] => -92.786144296672
)

)

[total] => 736
[businesses] => Array
(
[0] => stdClass Object
(
[is_claimed] => 1
[rating] => 4
[mobile_url] => http://m.yelp.com/bizzzz?utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=toQu_qgvu90-Z7dQuZOWMQ
[rating_img_url] => https://s3-media4.fl.yelpcdn.com/assets/2/www/img/c2f3dd9799a5/ico/stars/v1/stars_4.png
[review_count] => 147
[name] => Name here
[rating_img_url_small] => https://s3-media4.fl.yelpcdn.com/assets/2/www/img/f62a5be2f902/ico/stars/v1/stars_small_4.png
[url] => http://www.yelp.com/biz/zzz?utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=toQu_qgvu90-Z7dQuZOWMQ
[categories] => Array
(
[0] => Array
(
[0] => Chinese
[1] => chinese
)
)

[phone] => 5123355555
[snippet_text] => My family and I went to HAO-Q Asian Kitchen for the first time before a performance of our children in Aladdin. We all happen really love Asian cuisine....
[image_url] => https://s3-media3.fl.yelpcdn.com/bphoto/XS5NjGCdn3s14_efs9w5rw/ms.jpg
[snippet_image_url] => http://s3-media4.fl.yelpcdn.com/photo/ZxVY3kdLGl6AyAblYbIRgQ/ms.jpg
[display_phone] => +1-512-338-5555
[rating_img_url_large] => https://s3-media2.fl.yelpcdn.com/assets/2/www/img/ccf2b76faa2c/ico/stars/v1/stars_large_4.png
[id] => kitchen-austin
[is_closed] =>
[location] => stdClass Object
(
[city] => Austin
[display_address] => Array
(
[0] => 123 Street
[1] => Ste 113
[2] => Far West
[3] => Austin, TX 78731
)

[geo_accuracy] => 8
[neighborhoods] => Array
(
[0] => Far West/Northwest Hills
)

[postal_code] => 78731
[country_code] => US
[address] => Array
(
[0] => 3742 Far W Blvd
[1] => Ste 113
)

[coordinate] => stdClass Object
(
[latitude] => 31.356237
[longitude] => -92.758041
)

[state_code] => TX
)

)

我想使用限制输出一些结果:

$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10);

for ($x = 0; $x <= $limit; $x++) {

}

输出很好,但对于循环的每次迭代和每个值,我也不断收到以下错误:

Notice: Undefined offset: 10 in /mypath/YelpTest.php on line 94 Notice: Trying to get property of non-object in /mypath/YelpTest.php on line 94

我在这样的线路上是:

echo $response->businesses[$x]->name;

我错过了什么?

最佳答案

您正试图从只有 9 个元素的数组中获取第 10 个元素;您不依赖于可能小于您的限制的目标数组大小。

如果我试图获取有限数量的记录,我会从 $response->businesses 中获取一个切片并通过该切片执行 foreach:

$limit = isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10;

foreach (array_slice($response->businesses, 0, $limit) as $business) {
echo $business->name;
}

array_slice函数有一个很好的特性:当 $limit 大于数组元素的总数时(从 $offset 开始,函数的第二个参数)它只返回可用的元素.例如,如果您执行 array_slice([1, 2, 3, 4], 0, 100),它会返回 [1, 2, 3, 4] — limit是100,但是array只有4个元素,所以全部返回,切片时没有向array添加任何元素。

另一种方式:

$limit = isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10;

$counter = 0;

foreach ($response->businesses as $business) {
echo $business->name;

$counter++;

if ($counter >= $limit) {
break;
}
}

这里我只是在达到限制时中断 foreach 循环。当我的数组元素少于 $limit 时,我不会出错。

从您的代码继承的另一种方法:

$limit = isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10;

for ($x = 0; $x < $limit; $x++) {
if (!array_key_exists($x, $request->businesses)) {
// We have reached the end of $request->businesses array,
// so break "for" loop.
break;
}

echo $business->name;
}

在任何情况下,您都应该依赖数组的大小,因为它有可能小于 $limit,这会导致您出错。

关于php - Yelp API 输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35777143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com