如何在PHP中显示JSON中的数组值
我在PHP代码下面使用
echo '{"aaData":'.json_encode($user_details).'}';
它返回输出
{"aaData": [
{"id":"31","name":"Elankeeran","email":"ekeeran@yahoo.com","activated":"0","phone":""}
]}
但我需要JSON输出,如下所示
{"aaData": [
{"31","Elankeeran","ekeeran@yahoo.com","0","1234"}
]}
任何人请帮忙。
答案 0 :(得分:7)
$rows = array();
foreach ($user_details as $row) {
$rows[] = array_values((array)$row);
}
echo json_encode(array('aaData'=> $rows));
输出:
{"aaData": [
["31","Elankeeran","test@yahoo.com","0","1234"],
["33","Elan","test@gmail.com","1",""]
]}
答案 1 :(得分:1)
echo '{"aaData":'.json_encode(array_values($user_details)).'}';
应该这样做
答案 2 :(得分:1)
您的PHP已经生成了有效的JSON。要从JavaScript访问其中的项目,请使用以下模式:
obj.aaData[0].name;
// Elankeeran
obj.aaData[0].email;
// email@yahoo.com