我是 php 文件中 mysql 查询的 json 编码结果。我的php脚本如下:
while ($row = mysql_fetch_array($result)) {
$to_encode[] = $row;
}
echo json_encode($to_encode);
我的输出
[
{
"0": "Variables",
"topic_name": "Variables",
"1": "pyt1.1",
"rdf_id": "pyt1.1",
"2": "Hello World",
"ex_name": "Hello World",
"3": "1",
"line_number": "1",
"4": "print (\"Hello World!\")",
"code": "print (\"Hello World!\")",
"5": "Some Comment",
"comment": "Some Comment"
}
]
在这里,正如您所看到的,每个备用行都是多余的。我不需要第 1、3、5 行...等等。
所需输出
[
{
"topic_name": "Variables",
"rdf_id": "pyt1.1",
"ex_name": "Hello World",
"line_number": "1",
"code": "print (\"Hello World!\")",
"comment": "Some Comment"
}
]
如何更改php脚本以获得所需的输出?
最佳答案
在您的情况下,您应该使用 mysql_fetch_array()带有 MYSQL_ASSOC 标志:
while ( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) )
文档中的重要内容:
Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.
您可以在这里找到更多信息:https://stackoverflow.com/a/8891552/5297359
关于php json_encode mysql 结果以特定方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34091414/