Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON file using PHP/Laravel [duplicate]

I have a JSON that I need to parse.

{
    "Room 251": {
        "calID": "[email protected]",
        "availMsg": "Open Computer Lab"
        },

    "Room 318": {
        "calID": "[email protected]",
        "availMsg": "Open Computer Lab"
        },

    "Room 319 (Friends Room)": {
        "calID": "[email protected]",
        "availMsg": "Available For Study"
        },

    "Room 323": {
        "calID": "[email protected]",
        "availMsg": "Open Computer Lab"
        },

    "Room 513 (Voinovich Room)": {
        "calID": "[email protected]",
        "availMsg": "Available For Study"
        }
}

I need to obtain the room name, the calID, and the available message. What would be the best way to go about doing this in PHP/Laravel?

like image 923
Zach Tackett Avatar asked Aug 31 '26 03:08

Zach Tackett


1 Answers

You can use json_decode to parse a json data.

mixed json_decode ( string $json [, bool $assoc ] )

For example:

$rooms = json_decode($yourJsonHere, true);

var_dump($rooms);

foreach($rooms as $name => $data) {
    var_dump($name, $data['calID'], $data['availMsg']); // $name is the Name of Room
}
like image 150
Lucas Martins Avatar answered Sep 01 '26 16:09

Lucas Martins