PHP-使用预先固定的标记保存JSON

PHP-使用预先固定的标记保存JSON,php,mysql,json,mysqli,Php,Mysql,Json,Mysqli,现在,我有了这个MySQL数据集,它使用。它很好用 $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

现在,我有了这个MySQL数据集,它使用。它很好用

$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."
]
这样,它更易于阅读和理解

谢谢。

只需将其更改为:

$result = $resultSet->fetch_all(MYSQLI_ASSOC);
只需将其更改为:

$result = $resultSet->fetch_all(MYSQLI_ASSOC);