How to code json with multiple objects in PHP

1

I need the following result:

{
    "sala": [{
        "horario": "18:30",
        "data": "Seg e Terça"
    }, {
        "horario": "10:30",
        "data": "Quarta Terça"
    }]
}
  
    

UPDATE

  

I'm doing this in PHP:

                 /*criando objeto vazio*/
                $object = new stdClass();
                /*passando array para o objeto vazio*/
                $object->salas = array();

                $quantidade= $_POST['quantidade'];//imagina que aqui tem 2
           for($i=0;$i<count($quantidade);$i++){ //aqui vai rodar 2x

                $horario = $_POST['horarios_'.$quantidade]; //ATUALIZAÇÃO
                $dias= $_POST['dias_'.$quantidade]; //ATUALIZAÇÃO

                /*percorrendo o objeto vazio*/  
                foreach ($object as $key => $value) {           
                    $object_data = new stdClass();
                    $object_data->horario = $horario;//valor da hora
                    $object_data->data = $dias;//valor do dia
                    array_push($object->salas, $object_data);
                }

                /*codificando json*/    
                echo json_encode($object);

           }

But I'm getting it wrong:

{
    "salas": [{
        "horario": ["18:30", "10:30"],
        "data": ["Seg e Terça, Quarta Terça"]
    }]
}

Once you can generate the correct structure, how can I print the information according to each schedule and date     

asked by anonymous 25.08.2016 / 14:09

1 answer

1

The problem was that the variables $horario and $dias were passing an array, so when there was more than one loop in for it created a [] in json so I determined the [0] index % and worked.

    
25.08.2016 / 16:00