PHP - 使用 pre-fixed 标签保存 JSON
PHP - Save JSON with pre-fixed tag
现在,我有这个 MySQL 数据集,它使用 json_encode 保存到 JSON 文件。它工作正常。
$id = (int) $_GET['id'];
$sql = "SELECT answer1, answer2, answer3, answer4, explanation FROM questions ".
"WHERE quiz_id=?";
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param("i", $id);
$stmt->execute();
$resultSet = $stmt->get_result();
$json = array();
$result = $resultSet->fetch_all();
echo json_encode($result, JSON_PRETTY_PRINT);
$stmt->close();
}
这将输出到如下文件:
[
[
"Water",
"Theiving",
"Deception",
"The Underworld",
"Poseidon is one of the twelve Olympian deities of the pantheon in Greek mythology."
],
[
"To complete a challenge",
"To impress his wife",
"As an act of revenge",
"To defeat Hades",
"As an act of revenge, for being tricked by Prometheus, Zeus hid fire from mankind. Out of pity, Prometheus stole it back"
],
[
"A friend",
"His son",
"An enemy",
"His grandfather",
"He was overthrown by his own son Zeus and imprisoned in Tartarus. He ate his children and took advantage of his wife."
]
]
我的问题是,如何制作它才能在它前面加上这样的标题?
[
"answer_1": "A friend",
"answer_2": "His son",
"answer_3": "An enemy",
"answer_4": "His grandfather",
"explanation" : "He was overthrown by his own son Zeus and imprisoned in Tartarus. He ate his children and took advantage of his wife."
]
这样更read-able也更容易理解。
谢谢。
只需将其更改为:
$result = $resultSet->fetch_all(MYSQLI_ASSOC);
现在,我有这个 MySQL 数据集,它使用 json_encode 保存到 JSON 文件。它工作正常。
$id = (int) $_GET['id'];
$sql = "SELECT answer1, answer2, answer3, answer4, explanation FROM questions ".
"WHERE quiz_id=?";
if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param("i", $id);
$stmt->execute();
$resultSet = $stmt->get_result();
$json = array();
$result = $resultSet->fetch_all();
echo json_encode($result, JSON_PRETTY_PRINT);
$stmt->close();
}
这将输出到如下文件:
[
[
"Water",
"Theiving",
"Deception",
"The Underworld",
"Poseidon is one of the twelve Olympian deities of the pantheon in Greek mythology."
],
[
"To complete a challenge",
"To impress his wife",
"As an act of revenge",
"To defeat Hades",
"As an act of revenge, for being tricked by Prometheus, Zeus hid fire from mankind. Out of pity, Prometheus stole it back"
],
[
"A friend",
"His son",
"An enemy",
"His grandfather",
"He was overthrown by his own son Zeus and imprisoned in Tartarus. He ate his children and took advantage of his wife."
]
]
我的问题是,如何制作它才能在它前面加上这样的标题?
[
"answer_1": "A friend",
"answer_2": "His son",
"answer_3": "An enemy",
"answer_4": "His grandfather",
"explanation" : "He was overthrown by his own son Zeus and imprisoned in Tartarus. He ate his children and took advantage of his wife."
]
这样更read-able也更容易理解。
谢谢。
只需将其更改为:
$result = $resultSet->fetch_all(MYSQLI_ASSOC);