gmc1103 Posted October 7, 2019 Share Posted October 7, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309344-duplicates-in-complex-array/ Share on other sites More sharing options...
Barand Posted October 7, 2019 Share Posted October 7, 2019 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 ] Quote Link to comment https://forums.phpfreaks.com/topic/309344-duplicates-in-complex-array/#findComment-1570429 Share on other sites More sharing options...
gmc1103 Posted October 7, 2019 Author Share Posted October 7, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309344-duplicates-in-complex-array/#findComment-1570430 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.