php - 在数组内部创建 php json 对象数组

标签 php arrays json while-loop

我在尝试弄清楚如何创建一个在数组中包含一个数组的 Php json 对象时遇到了麻烦。我已经为此工作了几个小时,但无法弄清楚。我应该在我的 while 循环中使用 oject 并添加数组吗?

我想像这样在我的问题数组中包含我的答案数组。

{ 
"success":true,
"total":2,
"question":[
    {
        "id":"1",
        "product":"The Product",
        "question":"Some question here"
         "answer":[
         {
            "answer_id":"1",
            "answer":"First answer",
            "is_correct":"1",
            "question_id":"1"
         },
         {
            "answer_id":"2",
            "answer":"Second answer",
            "is_correct":"1",
            "question_id":"1"
         }
        ]
       }
      ],
      "question":[
    {
        "id":"2",
        "product":"The Product",
        "question":"Some question here"
         "answer":[
         {
            "answer_id":"1",
            "answer":"First answer",
            "is_correct":"0",
            "question_id":"1"
         },
         {
            "answer_id":"2",
            "answer":"Second answer",
            "is_correct":"1",
            "question_id":"1"
         }
        ]
       }
      ],

见下面的代码。

$question_arr = array();
$answer_arr = array();


    //Question table results
    $sql = "SELECT * FROM Questions WHERE product='".$product."'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
             $row_question_array['id'] = $row['ID'];
             $row_question_array['product'] = $row['product'];
             $row_question_array['question'] = $row['question'];

             array_push($question_arr,$row_question_array);


            //Anwser table results
            $sql2 = "SELECT * FROM Answers WHERE question_id='".$row['ID']."'";
            $result2 = $conn->query($sql2);

             while($row2 = $result2->fetch_assoc()) {


             $row_anwser_array['answer_id'] = $row2['answer_id'];
             $row_anwser_array['product'] = $row2['product'];
             $row_anwser_array['answer'] = $row2['answer'];
             $row_anwser_array['is_correct'] = $row2['is_correct'];
             $row_anwser_array['question_id'] = $row2['question_id'];

             array_push($answer_arr,$row_anwser_array);


          }

        }
    } else {
        echo "question 0 results";
    }


$myObj->success = true;             
$myObj->total = $result->num_rows;  
$myObj->question = $question_arr;   
$myObj->answer = $answer_arr;


//echo json_encode($question_arr);
//echo json_encode($answer_arr);
echo json_encode($myObj);               

最佳答案

无需创建两个单独的 $question_arr$answer_arr 数组。相反,只需创建一个空结果数组 $resultArr 并按以下方式重构您的代码,

$resultArr = array();
$sql = "SELECT * FROM Questions WHERE product='".$product."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $resultArr = array('success' => true, 'total' => $result->num_rows);
    while($row = $result->fetch_assoc()) {
        $resultArr['question'][$row['ID']] = array('id' => $row['ID'], 'product' => $row['product'], 'question' => $row['question']);

        //Anwser table results
        $sql2 = "SELECT * FROM Answers WHERE question_id='".$row['ID']."'";
        $result2 = $conn->query($sql2);
        while($row2 = $result2->fetch_assoc()) {
            $resultArr['question'][$row['ID']]['answer'][] = $row2;
        }
    }
    $resultArr['question'] = array_values($resultArr['question']);
} else {
    $resultArr = array('success' => false, 'total' => 0);
    echo "question 0 results";
}
echo json_encode($resultArr);   

关于php - 在数组内部创建 php json 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698301/

相关文章:

php - 添加表单上的 token 不匹配 laravel

php - 不要使用 JavaScript 提交 MYSQL 数据库中的现有值

python - Python 3.4 中的 Numpy 2D 数组

ios - 如何将 JSON 数据保存到 iPhone

json - Laravel:更新嵌套的 json 对象

php - 将mysql数据导出到csv文件

php - 如何在 Sonata Admin Bundle configureFormFields 中使用现有的 Symfony FormType?

php - 在 PHP 中合并 2 个数组

c - 在指向数组的指针中使用类型转换

jQuery Ajax setTimeout JSON