Jump to content
gmc1103

Duplicates in complex array

Recommended Posts

Hi

I have an array from an sql query and i would like to eliminates all the duplicates, i have google search for an answer without success, any help would be aprecciated

 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $final = array();
        $json = array();
        if ($idCoord > 0 || $isDirecao > 0) {
            foreach ($result as $row) {
                $idAtividade = $row['idAtividade'];
                if (!isset($final[$idAtividade])) {
                    $final[$idAtividade]['Escola'] = $row['Escola'];
                    $final[$idAtividade]['Atividade'] = $row['Atividade'];
                    $final[$idAtividade]['Periodo'] = $row['Periodo'];
                    $final[$idAtividade]['Mes'] = $row['Mes'];
                    $final[$idAtividade]['haveClasses'] = $row['haveClasses'];
                    $final[$idAtividade]['DataPrevista'] = $row['DataPrevista'];
                    $final[$idAtividade]['Destinatarios'] = $row['Destinatarios'];
                    $final[$idAtividade]['Orcamento'] = $row['Orcamento'];
                    $final[$idAtividade]['Organizador'] = $row['Organizador'];
                    $final[$idAtividade]['Obs'] = $row['Obs'];
                    $final[$idAtividade]['PdfAtividade'] = $row['PdfAtividade'];
                    $final[$idAtividade]['Avaliacao'] = $row['Avaliacao'];
                    $final[$idAtividade]['idProfessor'] = $row['idProfessor'];
                    $final[$idAtividade]['PdfAvaliacao'] = $row['PdfAvaliacao'];
                    $final[$idAtividade]['Validado'] = $row['Validado'];
                    $final[$idAtividade]['Nome'] = array();
                    $final[$idAtividade]['Grupo'] = array();
                    $final[$idAtividade]['Departamento'] = array();
                }
                $final[$idAtividade]['Nome'][] = $row['Nome'];
                $final[$idAtividade]['Grupo'][] = $row['Grupo'];
                $final[$idAtividade]['Departamento'][] = $row['Departamento'];

            }
            foreach ($final as $idVisita => $reservation) {
                $json[] = $reservation;
            }
        }
        echo json_encode($json);
    }

And this is an example i'm receiving

 

So you can see that Grupo has 4 times the value "500" and Departamento has 4 times that string...

How can avoid this and have only one value of each?

 

Thanks

 

 

array.png

Share this post


Link to post
Share on other sites

You don't show the query so I'm going to guess the results are from using a join.

If you have

tableA                                   tableB
+-----------+--------------+             +------+-----------+-------+
|  a_id     |   name       |             | b_id |   cola    | a_id  |
+-----------+--------------+             +------+-----------+-------+
|    1      |   Curly      |             |   1  |   aaa     |   1   |
|    2      |   Larry      |             |   2  |   bbb     |   2   |
+-----------+--------------+             |   3  |   ccc     |   1   |
                                         |   4  |   ddd     |   1   |
                                         |   5  |   eee     |   1   |
                                         |   5  |   fff     |   2   |
                                         |   5  |   ggg     |   2   |
                                         |   5  |   hhh     |   2   |
                                         +------+-----------+-------+
                                         
SELECT a.name
     , b_id
     , cola
FROM tablea a 
       INNER JOIN
     tableb b ON a.a_id = b.a_id
WHERE a.a_id = 1

then the results will be

+---------+--------+--------+
| name    |  b_id  |  cola  |
+---------+--------+--------+
| Curly   |    1   |   aaa  |
| Curly   |    3   |   ccc  |
| Curly   |    4   |   ddd  |
| Curly   |    5   |   eee  |
+---------+--------+--------+

where "Curly" is repeated 4 times because for the record in tablea there are 4 matching records in tableb with matching values in column a_id.

That's the nature of joins. Live with it.

A common way is to rearrange the data thus

data['Curly'] = [ 
                  1 => aaa,
                  3 => ccc,
                  4 => ddd,
                  5 => eee
                ]

 

Share this post


Link to post
Share on other sites

Hi Barand

Thanks for helping me

Sorry, i forgot to put the query

"SELECT t1.idAtividade, 
												t2.Escola,  
												t1.Atividade, 
                                                t1.Periodo, 
                                                t1.Mes, 
                                                t1.haveClasses,
                                                t1.DataPrevista,
                                                t1.Destinatarios, 
                                                t1.Orcamento, 
                                                t1.Organizador, 
                                                t1.Obs, 
                                                t1.PdfAtividade, 
                                                t1.Avaliacao, 
                                                t1.idProfessor, 
                                                t1.PdfAvaliacao,
                                                p.Nome,
                                                g.Grupo,
                                                d.Departamento,
                                                t1.Validado
                                                FROM atividades AS t1
                                                INNER JOIN atividadesprofessores ap on t1.idAtividade = ap.idAtividade
                                                INNER JOIN professores p on ap.idProfessor = p.idProfessor
                                                INNER JOIN atividadesgrupos ag on t1.idAtividade = ag.idAtividade
                                                INNER JOIN grupos g on ag.idGrupo = g.idGrupo
                                                INNER JOIN departamentosatividades da on t1.idAtividade = da.idAtividade
                                                INNER JOIN departamentos d on da.idDepartamento = d.idDepartamento
                                                INNER JOIN escolas AS t2 ON (t2.idEscola = t1.idEscola)
                                                INNER JOIN anosescolares AS ae ON (t1.idAnoEscolar = ae.idAnoEscolar)
                                                WHERE t1.idProjeto IS NULL AND ae.Estado = 1 ORDER BY t1.DataPrevista ASC

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.