10

Here's the json text:

{
"data": {
    "current_condition": [{
        "cloudcover": "75",
        "humidity": "63",
        "observation_time": "03:41 PM",
        "precipMM": "0.0",
        "pressure": "1020",
        "temp_C": "15",
        "temp_F": "59",
        "visibility": "16",
        "weatherCode": "116",
        "weatherDesc": [{
            "value": "Partly Cloudy"
        }],
        "weatherIconUrl": [{
            "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"
        }],
        "winddir16Point": "SSE",
        "winddirDegree": "160",
        "windspeedKmph": "7",
        "windspeedMiles": "4"
    }],
    "request": [{
        "query": "Northville, United States Of America",
        "type": "City"
    }],
    "weather": [{
        "date": "2013-09-24",
        "precipMM": "0.0",
        "tempMaxC": "20",
        "tempMaxF": "67",
        "tempMinC": "8",
        "tempMinF": "47",
        "weatherCode": "113",
        "weatherDesc": [{
            "value": "Sunny"
        }],
        "weatherIconUrl": [{
            "value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
        }],
        "winddir16Point": "ESE",
        "winddirDegree": "111",
        "winddirection": "ESE",
        "windspeedKmph": "10",
        "windspeedMiles": "6"
    }]
}

}

I'm trying to echo 'temp_F' and it is not working. I can't figure out what I'm doing wrong. I get this far:

$url = file_get_contents("http://blahblahblahblah");
$arr = json_decode($url,true);

And that's where it all fails. I've done var_dump's so I know the data is there. But every 'echo' attempt I've tried only results in 'Array' being displayed to the screen. I've tried many variations of the following:

echo $arr->{'data'}->{'current_condition[0]'}->{'temp_F'};

Can someone tell me what I'm doing wrong? Thanks!

1
  • You should actually see Trying to get property of non-object ... if your error reporting is activated. Commented Sep 24, 2013 at 16:06

4 Answers 4

20

json_decode() with TRUE as second parameter gives you an associative array. But you're currently trying to access it as an object.

Try the following:

echo $arr['data']['current_condition'][0]['temp_F'];
Sign up to request clarification or add additional context in comments.

Comments

1

That´s not how you access arrays in PHP

$array['index']="value";

echo $array['index1']['index2']

For your example:

echo $arr['data']['current_condition'][0]['temp_F']

Comments

0

You can use json to retrieve json result to a variable and then use variable information to display in js.

$.ajax({
    'type': 'GET',
    'url': 'abc.com,
    'dataType': 'json',
    success: function (data) {

    var response = data;
    // alert(response.data.current_condition)  //something like that
    // for (var i = 0; i < response.length; i++) { }

    }

Comments

0

You can use foreach loop condition like below:

$url = file_get_contents("http://blahblahblahblah");
$arr = json_decode($url,true);
foreach($arr as $data)  {
   echo $data['current_condition'][0]['temp_F'];
   echo $data['request'][0]['query'];
   echo $data['weather'][0]['date'];
}

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.