Multiple array in array

Hi guys

I am new in Slim, so I hope you can help me.

I have structure in POST request:

{
“siteID” : “41”,
“Room_0”:{
“roomID”: “62”,
“roomName”: “Room_1”,
},
“Room_1”:{
“roomID”: “63”,
“roomName”: “Room_2”,
},
“Room_2”:{
“roomID”: “64”,
“roomName”: “Room_3”,
},
“Room_3”:{
“roomID”: “65”,
“roomName”: “WC”,
}
}

routes.php file contain code:

$app->post(API_VERSION . API_POINT . ‘/test’, function() use ($app){
$data = json_decode($app->request->getBody(), true);
$app->render(‘test.php’, array(
‘siteID’ => $data[‘siteID’]
));
});

In test.php file I can use $siteID value and manipulate with it. But how to transmit Room_0[], Room_1[], etc arrays and values inside the arrays? There could be >=0 arrays with “Room_*” name

Your JSON is invalid, best to fix that too.

Wht not use json_decode to just return object:

$obj = json_decode($json);
echo $obj->Room_0->roomID;

One way (and a bad idea mind you, since you need to know the amount of items beforehand):

$obj = json_decode($json);

foreach (range(0, 30) as $i) {
    $v = 'Room_' . $i;
    if (isset($obj->$v)) {
        echo $obj->$v->roomID;
    }
}

Much better is it to output json like this:

{
   "siteID":"41",
   "Rooms":{
      "Room_0":{
         "roomID":"62",
         "roomName":"Room_1"
      },
      "Room_1":{
         "roomID":"63",
         "roomName":"Room_2"
      },
      "Room_2":{
         "roomID":"64",
         "roomName":"Room_3"
      },
      "Room_3":{
         "roomID":"65",
         "roomName":"WC"
      }
   }
}

Then you have access to $obj->Rooms and can work on its items.