Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating php json object array inside of array

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);               
like image 751
icode Avatar asked Apr 29 '26 12:04

icode


1 Answers

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);   
like image 108
Rajdeep Paul Avatar answered May 02 '26 01:05

Rajdeep Paul