133

I'm trying to get data from the following JSON file using PHP. I specifically want "temperatureMin" and "temperatureMax".

It's probably really simple, but I have no idea how to do this. I'm stuck on what to do after file_get_contents("file.json"). Some help would be greatly appreciated!

{
    "daily": {
        "summary": "No precipitation for the week; temperatures rising to 6° on Tuesday.",
        "icon": "clear-day",
        "data": [
            {
                "time": 1383458400,
                "summary": "Mostly cloudy throughout the day.",
                "icon": "partly-cloudy-day",
                "sunriseTime": 1383491266,
                "sunsetTime": 1383523844,
                "temperatureMin": -3.46,
                "temperatureMinTime": 1383544800,
                "temperatureMax": -1.12,
                "temperatureMaxTime": 1383458400,
            }
        ]
    }
}
Harold Dunn
  • 1,409
  • 2
  • 11
  • 9

3 Answers3

312

Get the content of the JSON file using file_get_contents():

$str = file_get_contents('http://example.com/example.json/');

Now decode the JSON using json_decode():

$json = json_decode($str, true); // decode the JSON into an associative array

You have an associative array containing all the information. To figure out how to access the values you need, you can do the following:

echo '<pre>' . print_r($json, true) . '</pre>';

This will print out the contents of the array in a nice readable format. Note that the second parameter is set to true in order to let print_r() know that the output should be returned (rather than just printed to screen). Then, you access the elements you want, like so:

$temperatureMin = $json['daily']['data'][0]['temperatureMin'];
$temperatureMax = $json['daily']['data'][0]['temperatureMax'];

Or loop through the array however you wish:

foreach ($json['daily']['data'] as $field => $value) {
    // Use $field and $value here
}

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Thanks! But I seem to be having a problem with the degree symbol in the JSON, am I doing something wrong? – Harold Dunn Nov 03 '13 at 22:46
  • @HaroldDunn: I don't understand what you mean by `degree symbol` -- could you clarify? – Amal Murali Nov 03 '13 at 22:48
  • 1
    "No precipitation for the week; temperatures rising to 6° on Tuesday." includes the degree symbol (°) in it. This seems to cause your demo to echo nothing when I try it on my site. – Harold Dunn Nov 03 '13 at 22:51
  • 1
    @HaroldDunn Care to share your solution? I suspect I may be having a similar problem... –  Dec 09 '15 at 22:06
  • 2
    @JonnyNineToes: Try setting `header('charset=utf8');` at the very top of your script. – Amal Murali Dec 10 '15 at 12:50
  • @Frayne Konok: Why did you remove the second parameter to `print_r()`? Read the documentation [here](http://php.net/print_r). It says _"If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it."._. – Amal Murali Mar 07 '18 at 09:24
  • @AmalMurali, This is very confusing for most of the users. – Murad Hasan Mar 07 '18 at 09:39
  • 1
    @FrayneKonok: They will be confused about the `1`. I think it's better to demonstrate the proper way to do it. I've also added a comment about the second parameter now. – Amal Murali Apr 09 '18 at 12:18
  • your given demo link is not working ... - Amal Murali – Ranjith Alappadan Oct 31 '18 at 06:06
  • @RanjithAlappadan: eval.in is down at the moment. It usually comes back up in a few hours. – Amal Murali Oct 31 '18 at 09:25
13

Use json_decode to transform your JSON into a PHP array. Example:

$json = '{"a":"b"}';
$array = json_decode($json, true);
echo $array['a']; // b
Guilherme Sehn
  • 6,727
  • 18
  • 35
13
Try:
$data = file_get_contents ("file.json");
        $json = json_decode($data, true);
        foreach ($json as $key => $value) {
            if (!is_array($value)) {
                echo $key . '=>' . $value . '<br/>';
            } else {
                foreach ($value as $key => $val) {
                    echo $key . '=>' . $val . '<br/>';
                }
            }
        }
Indrajeet Singh
  • 2,958
  • 25
  • 25