php and nested json: how can i access this element?

user2747609

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!

Amal Murali

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'];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how can I access to a specific element in json

From Dev

How do I access this JSON Element? (PHP)

From Dev

How can I access .json data in PHP?

From Java

How can I access and process nested objects, arrays or JSON?

From Dev

How can I access nested objets in JSON with JS / Ajax request

From Dev

how can I access a nested array within a json data return?

From Dev

How can I access to a nested objects

From Dev

How can I access the children of a panel element?

From Dev

How can I access the host of a custom element

From Dev

How can I access the host of a custom element

From Dev

How can I access the children of a panel element?

From Dev

How can I access a property of an html element?

From Dev

How do I access nested arrays from a json_decode in PHP?

From Dev

How can I deserialize nested JSON arrays?

From Dev

How can I simplify a nested php array?

From Dev

How can I access an individual element from JSON file via Angular4 HTTP Get?

From Dev

How can I access a particular element in an array of objects contained in JSON format using react?

From Dev

How can I access element reference from *ngFor loop element?

From Dev

can't access nested JSON

From Dev

How can I access my JSON object?

From Dev

How can I access index with @ in json

From Dev

How can I access values in this JSON?

From Dev

How can I access Objects in this JSON

From Dev

how can i access the value of a stdclass in php

From Dev

How can i access php complex array

From Dev

How do i access this Json with PHP

From Dev

how to add a nested element in already existing JSON with PHP

From Dev

How can I communicate between nested element directives?

From Dev

How can I pull out a nested grid element by 1 column?

Related Related

HotTag

Archive