Skip to content
Advertisement

Need to display only array value in JSON output

How to display only array value in JSON out in php

I am using below PHP code

echo '{"aaData":'.json_encode($user_details).'}';

And it return below output

{"aaData": [
    {"id":"31","name":"Elankeeran","email":"ekeeran@yahoo.com","activated":"0","phone":""}
]}

But I need JSON output like below

{"aaData": [
    {"31","Elankeeran","ekeeran@yahoo.com","0","1234"}
]}

Any one please help on this.

Advertisement

Answer

$rows = array();
foreach ($user_details as $row) {
  $rows[] = array_values((array)$row);
}

echo json_encode(array('aaData'=> $rows));

which outputs:

{"aaData": [
    ["31","Elankeeran","test@yahoo.com","0","1234"],
    ["33","Elan","test@gmail.com","1",""]
]}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement