Uniting arrays where they have a certain equal field

Asked

Viewed 86 times

1

How do I join several arrays in an array when they have the same fields?

inserir a descrição da imagem aqui

I have this following array being returned, I would like to group them into a single array where they have the same value pro id_question, for example: in the first 3 array that has the id_question = 1, they enter an array only, getting, array = [ [array1], [array2], [array3] ]

Q.S.: All of these arrays are inside of another that is contained in the $return variable

inserir a descrição da imagem aqui

the SQL is this: SELECT * FROM respostas INNER JOIN perguntas ON respostas.id_pergunta = perguntas.id_pergunta WHERE perguntas.id_questionario = $id_questionario

1 answer

0


Put, example:

while($result = $acesso->result->fetch(PDO::FETCH_ASSOC))
{    
    $retorno[$result['id_pergunta']][] = $result;
}

will group by key id_pergunta, this happens because the key is the same for that particular item of array, and the grouping happens naturally.

Reference: Arrays

Browser other questions tagged

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