Elankeeran
Elankeeran

Reputation: 6184

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.

Upvotes: 4

Views: 7718

Answers (3)

dorsh
dorsh

Reputation: 24750

$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",""]
]}

Upvotes: 7

Michael Berkowski
Michael Berkowski

Reputation: 270775

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]

Upvotes: 1

rauschen
rauschen

Reputation: 3996

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

should do it

Upvotes: 1

Related Questions