Create a json object for data table

0

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.

    
asked by Andrés 28.06.2017 в 23:16
source

1 answer

1

and I managed to solve it, I leave the answer in case someone else is presented with the same situation:

After obtaining the arrangement with the information of the query, a cycle should be carried out where the content of the data object that will be used with our datatable will be stored, in my case as I look for something completely dynamic inside the first cycle, I do another one that takes the name of the field, once armed the array with the data content, I made the creation of the array and ended up coding it in json as seen in the following code:

$prep   = $this->conexion->prepare($sql);  
$prep->execute();   
$result = $prep->fetchAll(PDO::FETCH_ASSOC);

$data   = [];
foreach ($result as $key => $value) {
    $InfoData=[]; 
    foreach ($value as $key1 => $value1) {
        $InfoData[$key1] = $value1;
    }
    $data[] = $InfoData;
}

$json_data = [
    "data"   => $data   
];

echo json_encode($json_data); 

Giving me the result:

{  
   "data":[  
      {  
         "id":"2",
         "fch":"2017-06-28 10:33:31.033",
         "cod":"AIGI",
         "val":"",
         "val2":"123",
         "ref":"1368879",
         "cant":"521"
      },
      {  
         "id":"3",
         "fch":"2017-06-28 15:54:20.185",
         "cod":"AIGI",
         "val":"",
         "val2":"123",
         "ref":"1368879",
         "cant":"521"
      }
   ]
}
    
answered by 29.06.2017 / 00:32
source