将 PHP 结果数组转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2122233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:08:12  来源:igfitidea点击:

Converting PHP result array to JSON

phpjson

提问by jspcal

I want to convert my result array to JSON format in PHP. Here is my code:

我想在 PHP 中将我的结果数组转换为 JSON 格式。这是我的代码:

$row = mysql_fetch_array($result)

I want to convert $rowto JSON format and pass the JSON data to an jQuery plugin.

我想转换$row为 JSON 格式并将 JSON 数据传递给 jQuery 插件。

回答by jspcal

json_encodeis available in php > 5.2.0:

json_encode在 php > 5.2.0 中可用:

echojson_encode($row);

echojson_encode($row);

回答by santhosh

$result = mysql_query($query) or die("Data not found."); 
$rows=array(); 
while($r=mysql_fetch_assoc($result))
{ 
$rows[]=$r;
}
header("Content-type:application/json"); 
echo json_encode($rows);