'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":"[email protected]","activated":"0","phone":""}
]}
But I need JSON output like below
{"aaData": [
{"31","Elankeeran","[email protected]","0","1234"}
]}
Any one please help on this.
Solution 1:[1]
$rows = array();
foreach ($user_details as $row) {
$rows[] = array_values((array)$row);
}
echo json_encode(array('aaData'=> $rows));
which outputs:
{"aaData": [
["31","Elankeeran","[email protected]","0","1234"],
["33","Elan","[email protected]","1",""]
]}
Solution 2:[2]
echo '{"aaData":'.json_encode(array_values($user_details)).'}';
should do it
Solution 3:[3]
Your PHP is already producing valid JSON. To access items in it from JavaScript, use patterns like:
obj.aaData[0].name;
// Elankeeran
obj.aaData[0].email;
// [email protected]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Elankeeran |
Solution 2 | rauschen |
Solution 3 | Michael Berkowski |