Get data from JSON file with PHP

Get the content of the JSON file using:

file_get_contents()

Definition and Usage

The file_get_contents() reads a file into a string.

This function is the best way to read the contents of a file into a string. It uses memory mapping if this technique is supported by the web server enhance performance.

Example completed:

<?php

// file_get_contents call instead
$str = '{
    "day": {
        "summary": "No precipitation for the day; temperatures rising to 8° on Monday.",
        "icon": "clear",
        "data": [
            {
                "time": 1383458420,
                "summary": "cloudy throughout the day.",
                "icon": "cloudy-day",
                "sunriseTime": 1383434266,
                "sunsetTime": 1383522344,
                "tempMin": -3.86,
                "tempMinTime": 1383567800,
                "tempMax": -1.18,
                "tempMaxTime": 1383467400
            }
        ]
    }
}';

// decode JSON
$json = json_decode($str, true);

// get the data
$temperatureMin = $json['day']['data'][0]['tempMin'];
$temperatureMax = $json['day']['data'][0]['tempMax'];

// echo it
echo $temperatureMin . PHP_EOL;
echo $temperatureMax;
Rate this post