Yelp API 输出错误
Errors with Yelp API output
我正在尝试使用 Yelp API2
做一些事情
$response = json_decode($data);
returns 数据以 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;
我错过了什么?
试试这段代码:
foreach($response->businesses as $key=>$r)
{
if($key>$limit) break;
echo $r->name //May need to do some var_dumps in order to see what you get
}
您似乎将限制默认为 10,但结果集中只能有 9 个对象。 Notice: Undefined offset: 10
告诉您 $response->businesses
数组中没有 10 的索引。 Notice: Trying to get property of non-object
告诉你 $response->businesses[10]
是空的,它没有任何可以读取的属性。
不要对值进行硬编码,而是尝试读取数组的大小:
$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : count($response->businesses);
for ($x = 0; $x <= $limit; $x++) {
...
您正在尝试从只有 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
开始,函数的第二个参数)它returns只有可用的元素。例如,如果你做 array_slice([1, 2, 3, 4], 0, 100)
,它 returns [1, 2, 3, 4]
— 限制是 100,但是数组只有 4 个元素,所以它们都被返回并且在切片时没有任何元素被添加到数组.
另一种方式:
$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
,这会导致您出错。
你有这个
$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10);
在它后面再加一个条件
$returnedBusinesses = count($response->businesses);
$limit = (($limit > $returnedBusinesses)?$returnedBusinesses:$limit);
for 循环条件还有一件事
现在
for ($x = 0; $x <= $limit; $x++) {
之后
for ($x = 0; $x < $limit; $x++) {
删除相等检查,因为我们是从 0 开始声明而不是 1。
我正在尝试使用 Yelp API2
做一些事情$response = json_decode($data);
returns 数据以 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;
我错过了什么?
试试这段代码:
foreach($response->businesses as $key=>$r)
{
if($key>$limit) break;
echo $r->name //May need to do some var_dumps in order to see what you get
}
您似乎将限制默认为 10,但结果集中只能有 9 个对象。 Notice: Undefined offset: 10
告诉您 $response->businesses
数组中没有 10 的索引。 Notice: Trying to get property of non-object
告诉你 $response->businesses[10]
是空的,它没有任何可以读取的属性。
不要对值进行硬编码,而是尝试读取数组的大小:
$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : count($response->businesses);
for ($x = 0; $x <= $limit; $x++) {
...
您正在尝试从只有 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
开始,函数的第二个参数)它returns只有可用的元素。例如,如果你做 array_slice([1, 2, 3, 4], 0, 100)
,它 returns [1, 2, 3, 4]
— 限制是 100,但是数组只有 4 个元素,所以它们都被返回并且在切片时没有任何元素被添加到数组.
另一种方式:
$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
,这会导致您出错。
你有这个
$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10);
在它后面再加一个条件
$returnedBusinesses = count($response->businesses);
$limit = (($limit > $returnedBusinesses)?$returnedBusinesses:$limit);
for 循环条件还有一件事
现在
for ($x = 0; $x <= $limit; $x++) {
之后
for ($x = 0; $x < $limit; $x++) {
删除相等检查,因为我们是从 0 开始声明而不是 1。