我使用php从mysql获取一行,然后使用以下代码将其编码为json
$jsonData = array();
if(mysqli_num_rows($result) > 0){
while ($array = mysqli_fetch_row($result)) {
$jsonData[] = $array;
}
$json = json_encode($jsonData);
echo stripslashes($json);
}`
但是,我只得到行值。我想要行值和它们的列名。目前它返回以下json。
[["shekhar","Shekhar Chatterjee","https://graph.facebook.com/1254850974526564/picture","0"]],[["shek","Shekhar Chatterjee","","0"]]
我想得到以下输出:
[{
"user":"shekhar",
"name":"Shekhar Chatterjee",
"url":"https://graph.facebook.com/1254850974526564/picture",
"stat":"0"
},{
"user":"shekhar",
"name":"Shekhar Chatterjee",
"url":"https://graph.facebook.com/1254850974526564/picture",
"stat":"0"
}]
最佳答案
使用mysqli_fetch_assoc()
干得好
$jsonData = array();
if(mysqli_num_rows($result) > 0){
while ($array = mysqli_fetch_assoc($result)) {
$jsonData[] = $array;
}
$json = json_encode($jsonData);
echo stripslashes($json);
}