[英]How to customize json_encode data in PHP?
我有一个名为questions
的 MySQL 数据库表,具有以下列:
id, question, category, type, difficulty, correct_answer, incorrect_answer_1, incorrect_answer_2, incorrect_answer_3
我需要将此表中的 output 数据转换为JSON
格式,但仅采用以下特定格式(如错误答案合并在一起):
{
"response_code": 0,
"results": [
{
"category": "Science & Nature",
"type": "multiple",
"difficulty": "hard",
"question": "An organic compound is considered an alcohol if it has what functional group?",
"correct_answer": "Hydroxyl",
"incorrect_answers": [
"Carbonyl",
"Alkyl",
"Aldehyde"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "easy",
"question": "Which of the following is not a faction in Tom Clancy's The Division?",
"correct_answer": "CDC",
"incorrect_answers": [
"Cleaners",
"Last Man Batallion",
"Rikers"
]
},
{
"category": "Entertainment: Video Games",
"type": "multiple",
"difficulty": "medium",
"question": "Which of the following Call of Duty games was a PS3 launch title?",
"correct_answer": "Call of Duty 3",
"incorrect_answers": [
"Call of Duty 4: Modern Warfare",
"Call of Duty: World at War",
"Call of Duty: Roads to Victory"
]
}
]
}
With the basic knowledge of PHP and json_encode
, I can output the data in JSON format, but unable to output it in the above-mentioned format. 我的 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_encode
,但我无法制作所需的格式,特别是使不正确的答案成为类似嵌套数组的格式。
我应该怎么办?
您在想要的 output 中有字段,结果中没有返回,因此您必须自己构建。
这只是一个指导性的答案! 这不会产生完整的输出! 您必须自己处理详细信息,尤其是对于incorrect_answers
的答案。
$results = [
'response_code' => 0,
'results' => []
];
foreach($outp as $item) {
$results['results'][] = $item;
}
echo json_encode($results, JSON_PRETTY_PRINT);
基本上,您希望一次检索一个结果集中的行(例如,在while
循环中),然后将检索到的行转换为您想要的格式并将其添加到output
变量中。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.