小编She*_*jee的帖子

使用列名从MySQL返回JSON

我使用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);
}`
Run Code Online (Sandbox Code Playgroud)

但是,我只得到行值.我想要行值和它们的列名.目前它返回以下JSON.

[["shekhar","Shekhar Chatterjee","https://graph.facebook.com/1254850974526564/picture","0"]],[["shek","Shekhar Chatterjee","","0"]]
Run Code Online (Sandbox Code Playgroud)

我想有以下输出:

[{
  "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"
}]
Run Code Online (Sandbox Code Playgroud)

php mysql arrays json

7
推荐指数
1
解决办法
2377
查看次数

标签 统计

arrays ×1

json ×1

mysql ×1

php ×1