I am currently working on a project where the datatable library is used, the query is done using PDO where I already bring the arrangement with the records of the query.
So I bring the records of the consultation with PDO:
$prep = $this->conexion->prepare($sql);
$prep->execute();
$result = $prep->fetchAll(PDO::FETCH_ASSOC);
$json = [
"data" => $result
];
echo json_encode($json,JSON_FORCE_OBJECT);
Seeing the example on the datatble page, the file I need handles this structure:
{
"data": [
{
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011/07/25",
"office": "Tokyo",
"extn": "8422"
}
]
}
but when using json_encode in php I only get this:
{
"data":{
"0":{
"id":"2",
"fch":"2017-06-28 10:33:31.033",
"cod":"AIGI",
"val":"",
"val2":"123",
"ref":"1368879",
"cant":"521"
},
"1":{
"id":"3",
"fch":"2017-06-28 15:54:20.185",
"cod":"AIGI",
"val":"",
"val2":"1234",
"ref":"1368879",
"cant":"521"
}
}
}
I already tried adding the JSON_FORCE_OBJECT option but I still can not get it, thank you in advance for taking the time to review it.