I am formating an array data into Json_encode using php. This array data is from my database. I have described how I am doing it here
$pens=$db->fetchAllPens(); //This fetches the list of pens
$a = array();
$response = array();
$response["success"]="true";
while ($pen = mysqli_fetch_array($pens)) {
$response["label"]["id"]=$pen["ID"];
$response["label"]["name"] = $pen["name"];
array_push($a,$response);
}
echo json_encode($a,JSON_PRETTY_PRINT);
The above code gives me the below output
[
{
"success": "true",
"label": {
"id": "1",
"name": "nimble"
}
},
{
"success": "true",
"label": {
"id": "2",
"name": "lopsel"
}
}
]
However I am expecting an output below
{
"success":true,
"label":[
{
"id":1,
"name":"nimble"
},
{
"id":2,
"name":"lopsel"
}
]
}
Please is there a way to achieve the desired results.