I am trying to do something with Yelp API2
$response = json_decode($data); which returns data in a PHP object format 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 a few results using limit:
$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10); for ($x = 0; $x <= $limit; $x++) { } The output is just fine but I also keep getting the following errors for each iteration of the loop and every 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 line is:
echo $response->businesses[$x]->name; What am I missing?
4 Answers
Answers 1
It looks like your defaulting the limit to 10 however there must only be 9 objects in the result set. The Notice: Undefined offset: 10 is telling you that there's no index for 10 in the $response->businesses array. The Notice: Trying to get property of non-object is telling you that $response->businesses[10] is null, which does not have any properties that can be read.
Instead of hard-coding a value, try reading the size of the array:
$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : count($response->businesses); for ($x = 0; $x <= $limit; $x++) { ... Answers 2
You are trying to get 10th element from array having only, for example, 9 elements; you do not rely on target array size which can be lesser than your limit.
If I were trying to get limited amount of records, I would get a slice from the $response->businesses and perform foreach through the 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 feature: when the $limit is greater than total count of array elements (starting from $offset, the second parameter of the function) it returns only available elements. For example, if you do array_slice([1, 2, 3, 4], 0, 100), it returns [1, 2, 3, 4] — limit is 100, but array has only 4 elements, so they all are returned and no any elements are added to array while 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 foreach loop when limit is reached. And I will have no errors when I have less than $limit array elements.
Another one approach which is inherited 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 array's size because it has chances to be lesser than $limit which will lead you to some errors.
Answers 3
You have this
$limit = (isset($_POST['displayLimit']) ? $_POST['displayLimit'] : 10); Add one more condition after it
$returnedBusinesses = count($response->businesses); $limit = (($limit > $returnedBusinesses)?$returnedBusinesses:$limit); One more thing the for loop condition
NOW
for ($x = 0; $x <= $limit; $x++) { AFTER
for ($x = 0; $x < $limit; $x++) { Remove equal check because we are stating from 0 not 1.
Answers 4
Try with 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 comments:
Post a Comment