Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
Creando una matriz de objetos php json dentro de una matriz

Tengo problemas para tratar de averiguar cómo crearía un objeto Php json que tenga una matriz dentro de la matriz. He estado trabajando en esto durante horas y no puedo resolverlo. ¿Debo usar oject dentro de mi ciclo while y agregar una matriz?

Me gustaría tener mi matriz de respuestas dentro de mi matriz de preguntas como esta.

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

Vea el código a continuación.

 $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);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No hay necesidad de crear dos matrices separadas $question_arr o $answer_arr . En su lugar, simplemente cree una matriz de resultados vacía $resultArr y refactorice su código de la siguiente manera:

 $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);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!