0
How to add an array within another array ? I’ve got these two, I want to add the bottom one on the top, with the name "answers"
Array
(
    [0] => Array
        (
            [id] => 0
            [unidade] => 4
            [exercicio] => 1
            [enunciado] => Complete with the correct pronoun
            [pergunta] => Marcus and his brother talk about politics between ???
            [imagem] => 
            [tipo] => complete1
            [respostacorreta] => themselves
        )
    [] => Array
        (
            [0] => They
            [1] => Them
            [2] => Themselves
        )
)
My final json result should look something like:
    {
    "0": {
        "id": "0",
        "unidade": "4",
        "exercicio": "1",
        "enunciado": "Complete with the correct pronoun",
        "pergunta": "Marcus and his brother talk about politics between ???",
        "imagem": null,
        "tipo": "complete1",
        "respostas": [
                      "They",
                      "Them",
                      "Themselves"
                  ],
        "respostacorreta": "themselves"
    }
}
My current code is like this:
$sql = new Sql();
$exercicioarray = $sql->select("SELECT * FROM tb_exercicios");
$arrayrespostas = explode(',',$exercicioarray[0]['respostas']);
unset($exercicioarray[0]['respostas']);
$exercicioarraysemresposta = $exercicioarray;
$exercicioarraycomresposta = $exercicioarraysemresposta[$exercicioarraysemresposta[0]['respostas']] = $arrayrespostas;
//print_r($exercicioarraysemresposta);
//print_r($arrayrespostas);
$response = json_encode($exercicioarraysemresposta, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
						
It worked, thanks. I think I understood the logic, the problem now is to treat this index of the array, because the database will return me several arrays of these. but I already have an idea with loop
– Igor Oliveira
what would it be like to return everything in the same SQL query? my database is structured the way q the final json gets a porem that the answers is a string separated by commas
– Igor Oliveira
@Igoroliveira Based on this new information, you only need the
$exercicioarray. Basically stop doing theunsetand in place make$exercicioarray[0]['respostas'] = $arrayrespostas;. Which looks better to iterate in a loop, something likefor($i=0; $i < count($arrayrespostas); $i++) $exercicioarray[$i]['respostas'] = explode(',',$exercicioarray[$i]['respostas']);– Juven_v
Very good, exactly what I needed, Thanks Juven_v
– Igor Oliveira