如何在PHP中定制json_编码数据?
我有一个名为如何在PHP中定制json_编码数据?,php,Php,我有一个名为questions的MySQL数据库表,包含以下列: id、问题、类别、类型、难度、正确答案、错误答案1、错误答案2、错误答案3 我需要将此表中的数据输出为JSON格式,但仅以以下特定格式输出(如将错误答案组合在一起): 有了PHP和json_encode的基本知识,我可以以json格式输出数据,但无法以上述格式输出。我的PHP代码是: $mysqli = new mysqli("localhost", "root", ""
questions
的MySQL数据库表,包含以下列:
id、问题、类别、类型、难度、正确答案、错误答案1、错误答案2、错误答案3
我需要将此表中的数据输出为JSON
格式,但仅以以下特定格式输出(如将错误答案组合在一起):
有了PHP和json_encode
的基本知识,我可以以json格式输出数据,但无法以上述格式输出。我的PHP代码是:
$mysqli = new mysqli("localhost", "root", "", "demo_db");
$statement = $mysqli->prepare("SELECT * FROM questions limit 50");
$statement->execute();
$result = $statement->get_result();
$outp = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($outp); // Tried this, but it's not outputting in desired format
//echo json_encode(($result->fetch_assoc())); // Tried this, but it's also not outputting in desired format
//echo json_encode(($result->fetch_array())); // Tried this, but it's also not outputting in desired format
我尝试了三种不同的方法来使用json\u encode
,但我无法生成所需的格式,尤其是将不正确的答案设置为类似嵌套数组的格式
我该怎么办?您想要的输出中有字段,这些字段不会在结果中返回,因此您必须自己构建 这只是一个指导性的答案这不会产生完整的输出!您必须自己处理详细信息,尤其是对于回答不正确的
$results=[
“响应代码”=>0,
“结果”=>[]
];
foreach($outp作为$item){
$results['results'][]=$item;
}
echo json_encode($results,json_PRETTY_PRINT);
基本上,您希望一次检索结果集中的一行(例如,在while
循环中),然后将检索到的行转换为所需的格式,并将其添加到输出
变量中
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "root", "", "demo_db");
$sql = "
SELECT
id,
question,
category,
type,
difficulty,
correct_answer,
incorrect_answer_1,
incorrect_answer_2,
incorrect_answer_3
FROM questions
Limit 50
";
$query = $mysqli->prepare($sql);
$query->execute();
$result = $query->get_result();
$output = [];
// Loop through the result set row by row
while ($row = $result->fetch_assoc()) {
$output[] = [
"category" => $row["category"],
"type" => $row["type"],
"difficulty" => $row["difficulty"],
"question" => $row["question"],
"correct_answer" => $row["correct_answer"],
"incorrect_answers" => [
$row["incorrect_answer_1"],
$row["incorrect_answer_2"],
$row["incorrect_answer_3"],
],
];
}
// Convert to JSON and print
echo json_encode($output);
谢谢你的答复,先生。我还考虑为response\u code
添加硬编码值。但正如您所提到的,$results[]=$item
,它不会在result
中添加值,而是输出如下内容(在json对象之前添加额外的数字)-“{response\u code:0,“results”:[],“0”:{“id”:1…},“1”:{“id”:2…
我的错,我拼错了。请看一下更新的答案。@halfer谢谢你的格式建议。这很有意义。谢谢你“不正确的答案”=>[$row[“不正确的答案”],…
这是我想知道的。
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "root", "", "demo_db");
$sql = "
SELECT
id,
question,
category,
type,
difficulty,
correct_answer,
incorrect_answer_1,
incorrect_answer_2,
incorrect_answer_3
FROM questions
Limit 50
";
$query = $mysqli->prepare($sql);
$query->execute();
$result = $query->get_result();
$output = [];
// Loop through the result set row by row
while ($row = $result->fetch_assoc()) {
$output[] = [
"category" => $row["category"],
"type" => $row["type"],
"difficulty" => $row["difficulty"],
"question" => $row["question"],
"correct_answer" => $row["correct_answer"],
"incorrect_answers" => [
$row["incorrect_answer_1"],
$row["incorrect_answer_2"],
$row["incorrect_answer_3"],
],
];
}
// Convert to JSON and print
echo json_encode($output);