使用PhP向json对象添加数组

使用PhP向json对象添加数组,php,json,Php,Json,我想在从数据库检索问题时对问题的答案数组进行编码 所需结果: [{"question":"What Barks","answers":["Cats","Dogs","Birds","Elephants"],"correct":1}] 源代码 require_once("connect.php"); $sql = "SELECT questions.question, GROUP_CONCAT(answers.answer) answers, questions.correct FROM que

我想在从数据库检索问题时对问题的答案数组进行编码

所需结果:

[{"question":"What Barks","answers":["Cats","Dogs","Birds","Elephants"],"correct":1}]
源代码

require_once("connect.php");
$sql = "SELECT questions.question, GROUP_CONCAT(answers.answer) answers, questions.correct FROM questions,answers  where answers.questionID = questions.questionID group by questions.question";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
}

echo json_encode($data);

mysqli_close($conn);
当前结果:

[{"question":"What Barks?","answers":"Cat,Dog,Fish,Cow","correct":"1"}]

更改mysql查询,如下所示:

> SELECT questions.question, 
> questions.correct,GROUP_CONCAT(answers.answer) answers FROM
> questions,answers  where answers.questionID = questions.questionID
> group by questions.question
这里我们使用MySQL中的一个GROUP_CONCAT聚合函数


.

如下更改mysql查询:

> SELECT questions.question, 
> questions.correct,GROUP_CONCAT(answers.answer) answers FROM
> questions,answers  where answers.questionID = questions.questionID
> group by questions.question
这里我们使用MySQL中的一个GROUP_CONCAT聚合函数


.

以下是基于您问题中的示例的示例解决方案:

require_once("connect.php");
$sql = 
    "SELECT questions.question, GROUP_CONCAT(answers.answer) answers, questions.correct FROM questions, answers WHERE answers.questionID = questions.questionID GROUP BY questions.question";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $a = array(
            "question"=>$row["question"],
            "answers"=>explode(",", $row["answers"]),
            "correct"=>$row["correct"]
        );
        $data[] = $a;
    }
}
echo json_encode($data);
mysqli_close($conn);

以下是基于您问题中的示例的示例解决方案:

require_once("connect.php");
$sql = 
    "SELECT questions.question, GROUP_CONCAT(answers.answer) answers, questions.correct FROM questions, answers WHERE answers.questionID = questions.questionID GROUP BY questions.question";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $a = array(
            "question"=>$row["question"],
            "answers"=>explode(",", $row["answers"]),
            "correct"=>$row["correct"]
        );
        $data[] = $a;
    }
}
echo json_encode($data);
mysqli_close($conn);

尝试从$sql更改=“选择questions.question,questions.correct,answers.answer From questions内部连接answers ON answers.questionID=questions.questionID”;TO$sql=“选择questions.question,questions.correct,answers.answers As answers from questions internal JOIN answers ON answers.questionID=questions.questionID”;您需要对数组进行格式化,以适应所需的结果。现在您正在对DB结果进行编码。我认为您的DB模式需要一些工作!您的数据甚至不是1n尝试从$sql更改为=“选择questions.question,questions.correct,answers.answers From questions内部连接answers ON answers.questionID=questions.questionID”;TO$sql=“选择questions.question,questions.correct,answers.answers As answers from questions internal JOIN answers ON answers.questionID=questions.questionID”;您需要对数组进行格式化,以适应所需的结果。现在您正在对DB结果进行编码。我认为您的DB模式需要一些工作!您的数据甚至没有1NF感谢您的回复,几乎按预期工作当前结果是生成[{“问题”:“什么叫?”,“答案”:“猫、狗、鱼、牛”,“正确”:“1”}]如何将其添加到数组中您可以尝试一下@Bradley
if(mysqli_num_rows($result)>0){$data=array();而($row=mysqli_fetch_assoc($result)){$answers=$row[“answers”];$row[“answers”]=explode(',',$answers);$data[]=$row;}
感谢您的回复,几乎可以正常工作当前结果是生成[{“问题”:“什么叫声?”,“答案”:“猫、狗、鱼、牛”,“正确”:“1”}]我如何将其添加到数组中?您可以尝试一下@Bradley
if(mysqli_num rows)吗($result)>0{$data=array();while($row=mysqli_fetch_assoc($result)){$answers=$row[“answers”];$row[“answers”]=explode(',',$answers);$data[]=$row;}