Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ericdiedrich's avatar

Updating JSON through input

Currently working on a booking system for meetings. I am trying to update my JSON structure with an input to change the meetings values such as: duration, numberOfPeople etc. I know that i should use


 $json = json_encode(file_get_contents($path), true);

{
        
    "pinkroom": {        
        "name": "The pink room",
        "tag": "pinkroom",
        "capacity": 5,
        "phone": true,
        "tv": false,
        "projector": true,
        "wheelchairacess": true,
        "meetings": [
        {
            "timeSlot": "9:00-9:30",
            "booked": true,
            "numberOfPeople": 2,
            "duration": 30,
            "meetingName": "React Front-End Meeting",
            "bookerName": "Eric Diedrichs"
        },
        {
            "timeSlot": "9:30-10:00",
            "booked": false,
            "numberOfPeople": null,
            "duration": null,
            "meetingName": null,
            "bookerName": null
        },


public function bookroom() {
        $path = storage_path() . "/json/meeting.json";
        $json = json_decode(file_get_contents($path), true);
        $selectedRoom = $_GET['meeting'];
        $roomDetails = [];

        foreach ($json as $key => $values) {
            if( $values["tag"] == $_GET['meeting']  ) {
                array_push($roomDetails, $values);
            }
        }

        $data = [ 
            'roomDetails' => $roomDetails,
            'selectedRoom' => $selectedRoom
        ];

        if( submit button pressed  ){
        send numberOfPeople to json
        send duration to json

        etc...

        }

        return view ('meetings.bookroom')->with($data);
    }


Would i just need to create an if statement inside my controller to check if input has been submited then json_encode?

0 likes
0 replies

Please or to participate in this conversation.