Incorrect format when generating JSON in PHP

1

I'm having trouble creating an array to pass it to JSON format.

When traversing the result of a query in the database, I assign the values to an array. After this, I format the JSON by putting a header to the list of values.

The problem is that for some reason an extra bracket appears, this makes then the html code does not work correctly.

This is my PHP code:

$arr = array();
$i = 0;
$response["MIGRUPO"] = array();

$resultUsuario = mysql_query("SELECT ID_GRUPO, 
                                     HORA
                                FROM GRUPOS");   

while ($row = mysql_fetch_assoc($resultUsuario)) {
    $arr[$i]['ID_GRUPO'] = $row['ID_GRUPO'];
    $arr[$i]['HORA'] = $row['HORA'];
    $i++;
}

array_push($response['MIGRUPO'], $arr);
$json_response = json_encode($response, JSON_PRETTY_PRINT);
echo $json_response;

This is the JSON that returns

"MIGRUPO":[
[
{
"ID_GRUPO":"1",
"HORA":"20:30:00"
},
{
"ID_GRUPO":"2",
"HORA":"20:30:00"
},
{
"ID_GRUPO":"3",
"HORA":"19:30:00"
},
{
"ID_GRUPO":"4",
"HORA":"18:30:00"
}
]
]
}

However, if I filter the query so that it only returns a record and only assigns values to a position in the array (by removing the $ i), the JSON is formatted correctly.

Can anyone see why one more dimension is generated?

    
asked by Jos Antonio Ruiz Santiago Fuse 05.11.2016 в 14:03
source

2 answers

1

You can avoid a dimension in the array by changing this line:

array_push($response['MIGRUPO'], $arr);

in which the entire array of results is being added as a single item in the array MIGRUPO , for the following:

$response['MIGRUPO'] = $arr;

where MIGRUPO is directly assigned to the result array $arr .

Result:

{
    "MIGRUPO": [
        {
            "ID_GRUPO": "1",
            "HORA": "20:30:00"
        },
        {
            "ID_GRUPO": "2",
            "HORA": "20:30:00"
        },
        {
            "ID_GRUPO": "3",
            "HORA": "19:30:00"
        },
        {
            "ID_GRUPO": "4",
            "HORA": "18:30:00"
        }
    ]
}

Demo on rextester.com

    
answered by 05.11.2016 / 14:39
source
0

the double array you generate in:

array_push($response['MIGRUPO'], $arr);

the clean code would be:

$response['MIGRUPO'] = array();
// No es necesario inicializar variables en PHP, sin embargo, es una muy
// buena práctica. Las variables no inicializadas tienen un valor 
// predeterminado de acuerdo a su tipo dependiendo del contexto en el 
// que son usadas
$resultUsuario = mysql_query('SELECT id_grupo, hora FROM grupos');

while ($row = mysql_fetch_assoc($resultUsuario)) {
    $response['MIGRUPO'][] = array('ID_GRUPO' => $row['id_grupo'], 'HORA' => $row['hora']);
}
echo json_encode($response, JSON_PRETTY_PRINT);

getting:

{
    "MIGRUPO": [
        {
            "ID_GRUPO": "1",
            "HORA": "20:30:00"
        },
        {
            "ID_GRUPO": "2",
            "HORA": "20:30:00"
        },
        {
            "ID_GRUPO": "3",
            "HORA": "19:30:00"
        },
        {
            "ID_GRUPO": "4",
            "HORA": "18:30:00"
        }
    ]
}

Greetings

    
answered by 05.11.2016 в 15:25