I have trouble sorting my array result in a different way. I have written an API call that returns specified results but not in the right way.
So it gives me:
{
"success": true,
"data": [
    [
        "Question",
        [
            "Answer"
        ]
    ],
    [
        "Question",
        [
            "Answer 2"
        ]
    ],
    [
        "Question 2",
        [
            "Answer 3"
        ]
    ]
],
"message": null
}
And I want to return a group of answers for that question like:
{
"success": true,
"data": [
    [
        "Question",
        [
            "Answer"
        ],
        [
            "Answer 2"
        ]
    ],
    [
        "Question 2",
        [
            "Answer 3"
        ]
    ]
],
"message": null
}
And my code looks like:
$questions = $this->getQRepository()->findAll();
$mappedQuestions = [];
foreach ($questions as $question){
    $title = $question->getTitle();
    $mappedQuestions[] = [
        $title,
        [
            $question->getAnswer()
        ]
    ];
}
return $mappedQuestions;
It gives me the result where it groups every question with answer by id but I need to group all answers by question. The result returns correctly but sorting is wrong.
 
     
     
    