I wrote an API where I return questions from one table and all the answers from another related to that question.
As it needs to sort them like multidimensional array I worked with logic but it throws an error on last "foreach",
My code:
$results = $this->getQuestionRepository()
->createQueryBuilder('a')
->getQuery()
->getResult();
$questionResults = $results[0];
$answers = $this->getAnswer($qresults); // gets all of the answers by question
$mappedQuestions = [];
foreach ($questionResults as $result){
$mappedQuestions[$result->getId()];
}
foreach ($questions as $question){
$questionId = $question->getQuestion()->getName();
$mappedQuestions[$questionId];
}
$results[0] = array_values($mappedQuestions);
return $results;
and it throws:
Notice: Undefined index: Name
and the problem is in
$mappedQuestions[$questionId];
As results in first dump query I get: