How to group array that has the same value

Asked

Viewed 136 times

0

I have the following string that is converted into an array that represents: "Question;Matter ID;Answer":

//PEGANDO AS REPOSTAS
$respostasgabarito='1;1;A|2;1;B|3;1;C|4;1;A|5;1;A|6;2;D|7;2;C|8;2;B|9;2;A|10;2;A';

//SEPARANDO QUESTÕES PELO CARACTER "|"
$questoesgabarito=explode('|',$respostasgabarito);

//FOREACH PARA CADA PERGUNTA PARA SEPARAR PERGUNTA, MATERIA E RESPOSTA
foreach($questoesgabarito as $reposta){ 
    $dividequestoes = explode(';',$reposta);
    $pergunta = $dividequestoes[0];
    $materia = $dividequestoes[1];
    $resposta = $dividequestoes[2];
}

How can I group the $materias which are the same? In this case, the responses of 1 to 5 would be grouped together and 6 to 10 would be grouped together.

2 answers

2

Just you set a array that will group the values, using the matter as key array, storing the values in the keys according to the subject of each item.

$gabarito = '1;1;A|2;1;B|3;1;C|4;1;A|5;1;A|6;2;D|7;2;C|8;2;B|9;2;A|10;2;A';

$materias = [];

foreach (explode('|', $gabarito) as $resposta) {
    list($pergunta, $materia, $alternativa) = str_getcsv($resposta, ';');

    if ( ! array_key_exists($materia, $materias)) {
        $materias[$materia] = [];
    }

    $materias[$materia][] = compact('pergunta', 'alternativa');
}

echo json_encode($materias, JSON_PRETTY_PRINT);

The result would be:

{
    "1": [
        {
            "pergunta": "1",
            "alternativa": "A"
        },
        {
            "pergunta": "2",
            "alternativa": "B"
        },
        {
            "pergunta": "3",
            "alternativa": "C"
        },
        {
            "pergunta": "4",
            "alternativa": "A"
        },
        {
            "pergunta": "5",
            "alternativa": "A"
        }
    ],
    "2": [
        {
            "pergunta": "6",
            "alternativa": "D"
        },
        {
            "pergunta": "7",
            "alternativa": "C"
        },
        {
            "pergunta": "8",
            "alternativa": "B"
        },
        {
            "pergunta": "9",
            "alternativa": "A"
        },
        {
            "pergunta": "10",
            "alternativa": "A"
        }
    ]
} 

See working on Repl.it | Ideone | Github GIST

  • show, but it’s like returning json_encode($materials, JSON_PRETTY_PRINT); it brings in 2 arrays?

  • 1

    Have, this you have the ability to do alone, just understand what the code does. I only converted to JSON to display a cleaner result.

1

Just add something like that into the for:

$resultado[$materia][] = ['pergunta' => $pergunta, 'resposta' => $resposta];

In the end it would look like:

//VALOR PADRÃO DO RESULTADO AGRUPADO
$resultado = [];

//PEGANDO AS REPOSTAS
$respostasgabarito='1;1;A|2;1;B|3;1;C|4;1;A|5;1;A|6;2;D|7;2;C|8;2;B|9;2;A|10;2;A'; 

//SEPARANDO QUESTÕES PELO CARACTER "|"
$questoesgabarito=explode('|',$respostasgabarito);

//FOREACH PARA CADA PERGUNTA PARA SEPARAR PERGUNTA, MATERIA E RESPOSTA
foreach($questoesgabarito as $reposta){ 
    list($pergunta, $materia, $resposta) = explode(';', $reposta);

    $resultado[$materia][] = ['pergunta' => $pergunta, 'resposta' => $resposta];        
}

Browser other questions tagged

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