Question

I'm using the yelp API in order to pull up random nearby listings for my website. From yelp's documentation on their website:

rating                 number   Rating for this business (value ranges from 1, 1.5, ... 4.5, 5)
rating_img_url         string   URL to star rating image for this business (size = 84x17)
rating_img_url_small   string   URL to small version of rating image for this business (size = 50x10)
rating_img_url_large   string   URL to large version of rating image for this business (size = 166x30)
snippet_text           string   Snippet text associated with this business
snippet_image_url      string   URL of snippet image associated with this business
location               dict     Location data for this business
location.address       list     Address for this business. Only includes address fields.
location.display_address list   Address for this business formatted for display. Includes all address fields, cross streets and city, state_code, etc.
location.city          string   City for this business
location.state_code    string   ISO 3166-2 state code for this business
location.postal_code   string   Postal code for this business
location.country_code  string   ISO 3166-1 country code for this business
location.cross_streets string   Cross streets for this business

How do I output the location variables such as location.display_address etc? My code below correctly outputs string and numbers by echoing "$response->businesses->rating". However, the last line of my code does not work.

// Handle Yelp response data
$response = json_decode($data);
$business = $response->businesses;

$numbers = range(0, 19);
shuffle($numbers);
echo "<img src='".$business[$numbers[$ran]]->image_url."'><br/>";
echo $business[$numbers[$ran]]->name."<br/>";
echo "<img border=0 src='".$business[$numbers[$ran]]->rating_img_url_large."'><br/>";
echo "<br/>";

echo $business[$numbers[$ran]]->location[display_address]."<br/>";

The error I get is

"Fatal error: Cannot use object of type stdClass as array in /home/content/38/11397138/html/yelpcall.php on line 51"

EDIT: var dump returns:

object(stdClass)#14 (6) { ["city"]=> string(11) "Chino Hills" ["display_address"]=> array(2) { [0]=> string(14) "2923 Chino Ave" [1]=> string(21) "Chino Hills, CA 91709" } ["postal_code"]=> string(5) "91709" ["country_code"]=> string(2) "US" ["address"]=> array(1) { [0]=> string(14) "2923 Chino Ave" } ["state_code"]=> string(2) "CA" }
Was it helpful?

Solution

Try var_dump($business[$numbers[$ran]]->location); this will tell you more about the variable you are using and help you to expose it's properties.

In your case, it looks like the location property is of type StdClass. Therefore access properties like so: $business[$numbers[$ran]]->location->display_address

Edit: Thank you for the update. The display_address property of the StdClass is a number indexed array therefore to access the elements of the array you can do this: $business[$numbers[$ran]]->location->display_address[0] But it looks like it was meant to be used more like this:

$address = $business[$numbers[$ran]]->location->display_address;
foreach( $address as $line ){
    echo $line.'<br />';
} 

OTHER TIPS

try echo $business[$numbers[$ran]]->location["display_address"]."
"; (adding quotes around the "display_address" key-name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top