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?