Add array inside array

0

How to add an array inside another array? I have these 2, I want to add the below in 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 result in json 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 looks 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);
    
asked by anonymous 12.01.2018 / 14:59

2 answers

2

Maybe you'd better return everything in the same SQL query, but since I do not have details of your database, you can not do it that way.

You have some mistakes in your code. Generally to add new elements in an array uses the following syntax:

//vazio
$array1 = [];
$array2 = [];

//para adicionar elementos com indice numerico
$array1[] = 1;
$array1[] = 2;
$array1[] = 3;

//no final $array1 = [1, 2, 3]

//para adicionar elementos com chave
$array2['campo1'] = 1;
$array2['campo2'] = 2;
$array2['campo3'] = 3;

//$array2 = ['campo1' => 1, 'campo2' => 2, 'campo3' => 3]

Applying to your code stays:

//se estiver na mesma linha da proxima instrução, 
//só o array respostas será copiado
$exercicioarraysemresposta[0]['respostas'] = $arrayrespostas;
$exercicioarraycomresposta = $exercicioarraysemresposta;

A more complete test:

<?php
 $exercicioarraysemresposta = [
     [
         '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',
         'respostacorreta' => 'themselves'
     ]
 ];

$arrayrespostas = [
    'They',
    'Them',
    'Themselves'
];

//se estiver na mesma linha da proxima instrução, só o array respostas será copiado
$exercicioarraysemresposta[0]['respostas'] = $arrayrespostas;
$exercicioarraycomresposta = $exercicioarraysemresposta;

var_dump($exercicioarraycomresposta);

echo json_encode($exercicioarraycomresposta);

?>

Generate as output json:

[
  {
    "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",
    "respostacorreta": "themselves",
    "respostas": [
      "They",
      "Them",
      "Themselves"
    ]
  }
]
    
12.01.2018 / 15:28
0

switch

$exercicioarraycomresposta = $exercicioarraysemresposta[$exercicioarraysemresposta[0]['respostas']] = $arrayrespostas;

by

 $exercicioarraycomresposta = $exercicioarraysemresposta;
 $exercicioarraycomresposta[0]['resposta'] = $arrayrespostas;
    
12.01.2018 / 15:14