Errors with the exit of the Yelp API

I'm trying to do something with Yelp API2

$response = json_decode($data); 

which returns data in the format of a PHP object that looks something like this:

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

I want to output some results using limit:

 $limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10); for ($x = 0; $x <= $limit; $x++) { } 

The result is just perfect, but I also keep getting the following errors for each iteration of the loop and each value:

 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 

And I have on such a line:

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

What am I missing?

+6
source share
4 answers

You are trying to get the 10th element from an array having only, for example, 9 elements; You do not rely on the size of the target array, which may be less than your limit.

If I were trying to get a limited number of entries, I would get a snippet from $response->businesses and execute foreach through a slice:

 $limit = isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10; foreach (array_slice($response->businesses, 0, $limit) as $business) { echo $business->name; } 

The array_slice function has a good function: when $limit greater than the total number of array elements (starting with $offset , the second parameter of the function), it returns only the available elements. For example, if you execute array_slice([1, 2, 3, 4], 0, 100) , it returns [1, 2, 3, 4] - limit is 100, but the array has only 4 elements, so they are all returned and no elements are added to the array when slicing.

Another way:

 $limit = isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10; $counter = 0; foreach ($response->businesses as $business) { echo $business->name; $counter++; if ($counter >= $limit) { break; } } 

Here I just break the foreach when the limit is reached. And I will not have errors if I have less elements of the $limit array.

Another approach that inherits from your code:

 $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; } 

In any case, you should rely on the size of the array, since it has a probability of being less than $limit , which will lead to some errors.

0
source

Try this piece of code:

 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 } 
0
source

It looks like your default limit is 10, but there should only be 9 objects in the result set. Notice: Undefined offset: 10 tells you that there is no index for 10 in the $response->businesses array. Notice: Trying to get property of non-object tells you that $response->businesses[10] is null, which has no properties that can be read.

Instead of hard coding the value, try reading the size of the array:

 $limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : count($response->businesses); for ($x = 0; $x <= $limit; $x++) { ... 
0
source

Do you have that

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

Add another condition after it

 $returnedBusinesses = count($response->businesses); $limit = (($limit > $returnedBusinesses)?$returnedBusinesses:$limit); 

Another condition for a loop cycle

now

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

after

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

Remove the equal check because we are specifying from 0 not 1.

0
source

All Articles