3

I'm having trouble trying to figure out how would I create a Php json object that has an array inside of array. I have been working on this for hours and can't figure it out. Should I use oject inside my while loop and add array?

I Would like to have my answer array inside my question array like this.

{ 
"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"
         }
        ]
       }
      ],

See code below.

$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);               

1 Answer 1

4

There's no need to create two separate $question_arr or $answer_arr arrays. Instead, just create one empty result array $resultArr and refactor your code in the following way,

$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);   
Sign up to request clarification or add additional context in comments.

1 Comment

@icode You're welcome! Glad I could help. Cheers! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.