Array is only returning the last index

Asked

Viewed 60 times

1

I created an array that has the following structure:

    $arr = array(
    "cabecalho" => [
        "instrucoes" => array("Instrucao 1", "Instrucao 2", "Instrucao 3", "Instrucao N"),
        "dadosAluno" => array("codigoAluno" => "123456789", "nomeCompleto" => "John Doe"),
        "dadosProva" => array(
            "curso" => "Administracao",
            "disciplina" => array("tipo" => "Modulo", "nome" => "Etica"),
            "periodoRealizacao" => array("inicio" => "2020-07-01", "fim" => "2020-07-31"),
            "periodoRealizacao2" => array("inicio" => "2020-07-01", "fim" => "2020-07-31"),
            "tempoDeProvaEmSegundos" => "3600"
        ),
        "UrlLogin" => "https://wwwexemplo.com/login/",
        "UrlRetorno" => "https://wwwexemplo.com/retorno/"
    ],
    "questoes" => [[
        "idExterno" => "1",
        "pergunta" => "Pergunta 1",
        "respostasPossiveis" => array(
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 1")),
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 1")),
        ),
        "idExterno" => "2",
        "pergunta" => "Pergunta 2",
        "respostasPossiveis" => array(
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 2")),
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 2")),
        ),
    ]],
);

When executing the command: echo '<pre><br />';print_r($arr['questoes']);echo '</pre>';die(); is returning the second structure

(
    [0] => Array
        (
            [idExterno] => 2
            [pergunta] => Pergunta 2
            [respostasPossiveis] => Array
                (
                    [0] => Array
                        (
                            [opcao] => A
                            [texto] => Resposta A da pergunta 2
                        )

                    [1] => Array
                        (
                            [opcao] => A
                            [texto] => Resposta A da pergunta 2
                        )
                )
        )
)

It is not displaying the first array that contains the idExterno equal to 1 and all its contents of question 1 is not displayed.

The last amount is overwriting the first or everyone there is before, how do I fix this?

1 answer

2


This is because the array has two keys called idExterno. Basically what you did was (omitting some data to make it simpler to view):

[[
    "idExterno" => "1",
    "pergunta" => "Pergunta 1",
    "respostasPossiveis" => array( ... ),
    "idExterno" => "2",
    "pergunta" => "Pergunta 2",
    "respostasPossiveis" => array( ... ),
]],

Note that all keys are in the same array, ie have two idExterno, two pergunta, etc..

If the idea was to have two elements, each with its own idExterno and other keys, then you would have to add the brackets in the correct place:


[
  [
    "idExterno" => "1",
    "pergunta" => "Pergunta 1",
    "respostasPossiveis" => array( ... )
  ],  <-- aqui termina o array com idExterno 1
  [   <-- aqui começa o outro array, com idExterno 2
    "idExterno" => "2",
    "pergunta" => "Pergunta 2",
    "respostasPossiveis" => array( ... ),
  ]
],

So it would look like this:

$arr = array(
    "cabecalho" => [
        "instrucoes" => array("Instrucao 1", "Instrucao 2", "Instrucao 3", "Instrucao N"),
        "dadosAluno" => array("codigoAluno" => "123456789", "nomeCompleto" => "John Doe"),
        "dadosProva" => array(
            "curso" => "Administracao",
            "disciplina" => array("tipo" => "Modulo", "nome" => "Etica"),
            "periodoRealizacao" => array("inicio" => "2020-07-01", "fim" => "2020-07-31"),
            "periodoRealizacao2" => array("inicio" => "2020-07-01", "fim" => "2020-07-31"),
            "tempoDeProvaEmSegundos" => "3600"
        ),
        "UrlLogin" => "https://wwwexemplo.com/login/",
        "UrlRetorno" => "https://wwwexemplo.com/retorno/"
    ],
    "questoes" => [
        [
          "idExterno" => "1",
          "pergunta" => "Pergunta 1",
          "respostasPossiveis" => array(
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 1")),
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 1")),
          )
        ],
        [
          "idExterno" => "2",
          "pergunta" => "Pergunta 2",
          "respostasPossiveis" => array(
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 2")),
            array("opcao" => "A", "texto" => utf8_encode("Resposta A da pergunta 2")),
          )
        ]
    ]
);

print_r($arr['questoes']);

And the way out will be:

Array
(
    [0] => Array
        (
            [idExterno] => 1
            [pergunta] => Pergunta 1
            [respostasPossiveis] => Array
                (
                    [0] => Array
                        (
                            [opcao] => A
                            [texto] => Resposta A da pergunta 1
                        )

                    [1] => Array
                        (
                            [opcao] => A
                            [texto] => Resposta A da pergunta 1
                        )

                )

        )

    [1] => Array
        (
            [idExterno] => 2
            [pergunta] => Pergunta 2
            [respostasPossiveis] => Array
                (
                    [0] => Array
                        (
                            [opcao] => A
                            [texto] => Resposta A da pergunta 2
                        )

                    [1] => Array
                        (
                            [opcao] => A
                            [texto] => Resposta A da pergunta 2
                        )

                )

        )

)
  • 1

    Thanks, it worked out!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.