6

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?

7
  • 2
    have you ever tried some thing to do this ? Commented Mar 10, 2017 at 13:57
  • Meaning what exactly? Commented Mar 10, 2017 at 13:58
  • have you tried any thing to parse json using laravel or php ? Commented Mar 10, 2017 at 14:00
  • json_decode() in PHP but I didn't know if there was a Laravel specific way of going about doing it. Commented Mar 10, 2017 at 14:01
  • 1
    $data = json_decode($data, TRUE); returns a multidimensional array. Any laravel specific way would be pointless as it would be an abstraction of this one liner Commented Mar 10, 2017 at 14:04

2 Answers 2

14

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

That gives all of the rooms as a multidimensional array, which I already have. I need to get the values from the rooms and set them to variables
Why set them to variables? Why can't you use array?
Because I need them to be variables to pass later on.
Okay, do as you wish.
Marked yours as correct as it had the correct solution to what I was needing even though i didn't need the data set into an array. Thanks
1

You can do something like that :

<?php
$json = '
{
    "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"
        }
}';
foreach(json_decode($json) as $room_name => $room){
  echo $room_name.'<br/>'; // output the room name, for instead "Room 251"
  echo $room->calID.'<br/>'; // output the room calID
  echo $room->availMsg.'<br/>'; // output the room availMsg
}

 ?>

5 Comments

Trying to get property of non-object
I've tested it, it works, are you sure you don't miss something while copy/paste it ?
$cal_ID = $keys['calID']; $availMsg = $keys['availMsg']; echo $cal_ID; echo "<br>"; echo $availMsg; echo "<br>";
don't you think that $data = json_decode($json); foreach ($data ..... would be better ? @VincentG
Maybe it could be better in performance term, but I'm not sure. It provides the same output result though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.