json_decode() function in PHP

 <?php


$json_string = "json/my.json";

$jsondata = file_get_contents($json_string);

$arr = json_decode($jsondata, true);

echo "<table border='1' cellpadding='10px' width='100%'>";

echo "<th>Id</th><th>Name</th><th>Age</th><th>City</th>";
foreach ($arr as list("id" => $id, "name" => $name, "age" => $age, "city" => $city)) {

    echo "<tr><td style='text-align: center'>{$id}</td><td style='text-align: center'>{$name}</td><td style='text-align: center'>{$age}</td><td style='text-align: center'>{$city}</td></tr>";
}

echo "</table>";

// echo "<pre>";
// print_r($arr);
// echo "</pre>";
Above File is index.php File.






Below File is json/my.json File
[
    {
        "id": "1",
        "name": "Yahoo Baba",
        "age": "25",
        "city": "Delhi"
    },
    {
        "id": "2",
        "name": "Salman Khan",
        "age": "24",
        "city": "Agra"
    },
    {
        "id": "3",
        "name": "Shahid Kapoor",
        "age": "24",
        "city": "Bhopal"
    },
    {
        "id": "4",
        "name": "Juhi Chawla",
        "age": "26",
        "city": "Mumbai"
    },
    {
        "id": "5",
        "name": "Katrina Kaif",
        "age": "23",
        "city": "Mumbai"
    }
]


Comments

Popular posts from this blog

Logical_Operators

SubQuery with EXISTS and NOT EXISTS in PHP

Get Functions