Trying to get property of non-object | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Trying to get property of non-object

//API REQUEST>>>>> ( 'criteria' => array ( 'additional' => array ( 'currency' => 'USD', 'dailyRate' => array ( 'maximum' => 10000, 'minimum' => 1, ), 'discountOnly' => false, 'language' => 'en-us', 'maxResult' => (int)$results, 'minimumReviewScore' => 0, 'minimumStarRating' => 0, 'occupancy' => array ( 'numberOfAdult' => (int)$adults, 'numberOfChildren' => 1, ), 'sortBy' => 'PriceAsc', ), 'checkInDate' => $in, 'checkOutDate' => $out, 'cityId' => (int)$hotel, ), ) //API RESPONSE>>>> { "results":[ { "hotelId":6501506, "hotelName":"Private room+bathroom (A2) / 10min walk MRT", "starRating":3.0, "reviewScore":8.6, "reviewCount":2, "currency":"USD", "dailyRate":9.91, "crossedOutRate":15.09, "discountPercentage":33.0, "imageURL":"http://pix6.agoda.net/hotelImages/6501506/0/29741738682c56e0c3ad1bf5f4a1f9b7.jpg?s=800x600", "landingURL":"https://www.agoda.com/partners/partnersearch.aspx?cid=1813200&hid=6501506€cy=USD&checkin=2019-06-08&checkout=2019-06-12&NumberofAdults=2&NumberofChildren=1&Rooms=1&pcs=6", "includeBreakfast":false, "freeWifi":false, "latitude":13.8136554, "longitude":100.56936369999994 } <?php $result = (array)json_decode($result); echo "<pre>"; foreach($result as $row){ echo $row->hotelName."<br>"; } ?>

23rd Apr 2019, 11:24 AM
Ahmed Siddiqui
Ahmed Siddiqui - avatar
1 Answer
0
I suppose you’re not getting anything out of your object, this would be because when you run your foreach-loop you’re not actually accessing the results. Within your $result variable, with the information you provided, there should be a ”result” key. So basically you’d need the following loop: foreach ($result->result as $row) Or simply access the key when you’re assigning your variable: (json_decode($result))->result
23rd Apr 2019, 7:50 PM
Victor Andersson
Victor Andersson - avatar