0

I'm trying to figure out how to echo the address1 out of the JSON below. I've tried this - echo "$arr->location[1]->address1<br>";, but it returns this error

Catchable fatal error: Object of class stdClass could not be converted to string in /home/benrud/public_html/student/webdesign/2016/02_benrud/tinker/data/index.php on line 202.

echo $arr; returns the JSON below.

{
  "photos": [
    "https://s3-media2.fl.yelpcdn.com/bphoto/37El1q8mqM_1tKtQugncZQ/o.jpg",
    "https://s3-media1.fl.yelpcdn.com/bphoto/GLsNPPz5do-_NJktIQvz6w/o.jpg",
    "https://s3-media3.fl.yelpcdn.com/bphoto/Z4rdHERgb10MZgDXnct5lA/o.jpg"
  ],
  "coordinates": {
    "latitude": 33.0479031276,
    "longitude": -117.256002333
  },
  "image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/37El1q8mqM_1tKtQugncZQ/o.jpg",
  "is_claimed": false,
  "id": "oscars-mexican-seafood-encinitas-2",
  "review_count": 48,
  "rating": 4.5,
  "hours": [
    {
      "hours_type": "REGULAR",
      "is_open_now": true,
      "open": [
        {
          "is_overnight": false,
          "end": "2100",
          "day": 0,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 1,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 2,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 3,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2200",
          "day": 4,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2200",
          "day": 5,
          "start": "0800"
        },
        {
          "is_overnight": false,
          "end": "2100",
          "day": 6,
          "start": "0800"
        }
      ]
    }
  ],
  "display_phone": "(760) 487-5778",
  "categories": [
    {
      "alias": "seafood",
      "title": "Seafood"
    },
    {
      "alias": "mexican",
      "title": "Mexican"
    }
  ],
  "price": "$",
  "phone": "+17604875778",
  "name": "Oscars Mexican Seafood",
  "location": {
    "zip_code": "92024",
    "address3": null,
    "address1": "115 N El Camino Real",
    "country": "US",
    "city": "Encinitas",
    "state": "CA",
    "cross_streets": "Via Molena & Encinitas Blvd",
    "display_address": [
      "115 N El Camino Real",
      "Encinitas, CA 92024"
    ],
    "address2": ""
  },
  "transactions": [],
  "url": "https://www.yelp.com/biz/oscars-mexican-seafood-encinitas-2?adjust_creative=YqqOIA_bNY3Qb_A1TRMMUg&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_lookup&utm_source=YqqOIA_bNY3Qb_A1TRMMUg",
  "is_closed": false
}
3
  • 1
    Can you please format the outputted json correctly? It's hard to follow in its current state. Commented Apr 3, 2017 at 20:27
  • 2
    Have you tried echo $arr->location->address1."<br>";? Commented Apr 3, 2017 at 20:29
  • Why are you doing $arr->location[1]? location is not an (numeric) array, it's an object. Commented Apr 3, 2017 at 20:29

3 Answers 3

2

Location isn't an array, so I'd imagine just $arr->location->address1.

Sign up to request clarification or add additional context in comments.

Comments

0
$arr = json_decode($json, true);

the true parameter makes sure it's an array and not an object

Comments

-1

You must first decode then call the key so:

// Decode the JSON STRING
$arr = json_decode($arr, true); 
/*
 * first argument is the JSON STRING, 
 * Second sets the flag that the string is a dictionary 
 * (associative array)
 */

Now it is time to call the element. I put it in a conditional so to prevent errors

if (array_key_exists('address1', $arr['location'])) {
    echo $arr['location']['address1'];
}
else {
    echo "Array element Not Found. Here is what I have:\n\r";
    print_r($arr);
}

This Should return your element's value OR Dump the parsed PHP Array for review so you can edit the if statement to get the correct location.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.