6

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?

santa
  • 13,148
  • 42
  • 144
  • 230

4 Answers4

0

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
}
Dimitrios Desyllas
  • 6,470
  • 6
  • 43
  • 106
  • Why you added if($r>$limit) break; in foreach? What you don't have any alternative or this is the best practice? – itzmukeshy7 Mar 11 '16 at 05:35
  • With tihs Implementation if $response->businesses has less data than limit it will stop traversing the array by itself. – Dimitrios Desyllas Mar 12 '16 at 10:52
  • Definitely by why we are adding an extra condition in every iteration if we can do the same with a single statement; see my answer; :) – itzmukeshy7 Mar 14 '16 at 05:03
0

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++) { 
    ...
Wyatt McGuire
  • 477
  • 3
  • 9
0

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.

Ronin
  • 1,608
  • 1
  • 12
  • 22
  • Why you are checking it in every loop if (!array_key_exists($x, $request->businesses)) {... ? – itzmukeshy7 Mar 11 '16 at 05:34
  • @itzmukeshy7 Umm... To make sure that `$request->businesses[$x]` is exists. :) It's because `for ($x = 0; $x < $limit; $x++)` does not rely on `$request->businesses` size at all. So, we need to make sure if array key `$x` exists therefore array has a value under that index. – Ronin Mar 11 '16 at 05:48
  • But why we are making it complex when we can do it just by updating the $limit with count($response->businesses) as per my answer – itzmukeshy7 Mar 11 '16 at 05:49
  • @itzmukeshy7 But also, why we are making it complex at all by calculating limits intersection? We may just perform a slice and do not worry about other things (see first example in my answer). – Ronin Mar 11 '16 at 06:01
0

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.

itzmukeshy7
  • 2,580
  • 1
  • 19
  • 28