gmc1103 Posted September 18, 2020 Share Posted September 18, 2020 Hello Can somepne help me with this issue I have a query who is returning some records ID disciplina moduloUfcd idcpDisciplinas anoTurma 58, Comunicação Visual, 8599, 49, 11 59, Comunicação Visual, 133, 49, 11 60, Comunicação Visual, 134, 49, 10 When i trying to put this into an array with just one line (merge all with number 49 (idcpDisciplinas) and (anoTurma) 11) i can't get the desired output My sql query is this one select c.idPlan, cD.disciplina, cM.moduloUfcd, p.Nome, t.Turma, a.Ano, c2.curso, c.validCron, c.cronograma, c.dataLimite, c.idProfessor,cM.horas,cM.tempos, c.pdfCronograma, c2.curso, cM.discModulo, cM.idcpDisciplinas, cM.ano as anoTurma from cpDiscProfessores c inner join cpDisciplinas cD on c.idcpDisciplinas = cD.idCpDisciplinas inner join cpModulos cM on c.idCpModulos = cM.idCpModulos inner join professores p on c.idProfessor = p.idProfessor left join turmas t on c.idTurma = t.idTurma inner join anosescolares a on c.idAnoEscolar = a.idAnoEscolar left join cursos c2 on c.idCursos = c2.idCursos where a.Estado = 1; And in php what i have is this $result = $stmt->fetchAll(PDO::FETCH_ASSOC); $final = array(); $json = array(); foreach ($result as $row) { $moduloUfcd= $row['moduloUfcd']; if (!isset($moduloUfcd[$moduloUfcd])) { $horas= $row['horas']; $final[$moduloUfcd]['idPlan'] = $row['idPlan']; $final[$moduloUfcd]['disciplina'] = $row['disciplina']; $final[$moduloUfcd]['Nome'] = $row['Nome']; $final[$moduloUfcd]['Turma'] = $row['Turma']; $final[$moduloUfcd]['anoTurma'] = $row['anoTurma']; $final[$moduloUfcd]['curso'] = $row['curso']; $final[$moduloUfcd]['validCron'] = $row['validCron']; $final[$moduloUfcd]['cronograma'] = $row['cronograma']; $final[$moduloUfcd]['dataLimite'] = $row['dataLimite']; $final[$moduloUfcd]['idProfessor'] = $row['idProfessor']; $final[$moduloUfcd]['pdfCronograma'] = $row['pdfCronograma']; $final[$moduloUfcd]['discModulo'] = $row['discModulo']; $final[$moduloUfcd]['idcpDisciplinas'] = $row['idcpDisciplinas']; $final[$moduloUfcd]['moduloUfcd'] = array(); } $final[$moduloUfcd]['moduloUfcd'][] = $row['moduloUfcd']; $final[$moduloUfcd]['horas'][] = $row['horas']; $final[$moduloUfcd]['tempos'][] = $row['tempos']; } foreach ($final as $moduloUfcd => $cron) { $json[] = $cron; } echo json_encode($json); What it gives is : [ { idPlan: "60", disciplina: "Comunicação Visual", Nome: "XXXXXXXXX", Turma: "11ºO", anoTurma: "11", curso: "Técnico de Audiovisuais", validCron: "-1", cronograma: "0", dataLimite: "2020-10-30", idProfessor: "168", pdfCronograma: "/XXXXX/pdf/profissionais/cronogramas/Cronograma -Técnico de Audiovisuais-8599.pdf", discModulo: "8599", idcpDisciplinas: "49", moduloUfcd: [ "8599" ], horas: [ "25" ], tempos: [ " 34 " ] } ] Quote Link to comment https://forums.phpfreaks.com/topic/311500-mysql-into-to-php-array-issue/ Share on other sites More sharing options...
gizmola Posted September 18, 2020 Share Posted September 18, 2020 Please provide an example of the array you are expecting to generate. Quote Link to comment https://forums.phpfreaks.com/topic/311500-mysql-into-to-php-array-issue/#findComment-1581461 Share on other sites More sharing options...
gmc1103 Posted September 18, 2020 Author Share Posted September 18, 2020 6 hours ago, gizmola said: Please provide an example of the array you are expecting to generate. Thanks for your reply I need this (58, 59 ), Comunicação Visual, ( 8599, 133) ,49, 11 60, Comunicação Visual, 134, 49, 10 Quote Link to comment https://forums.phpfreaks.com/topic/311500-mysql-into-to-php-array-issue/#findComment-1581466 Share on other sites More sharing options...
Barand Posted September 18, 2020 Share Posted September 18, 2020 If you are starting with this (which could be the result from a table subquery) ... +----+----------------------+------------+-----------------+----------+ | id | disciplina | moduloUfcd | idcpDisciplinas | anoTurma | +----+----------------------+------------+-----------------+----------+ | 58 | Comunicação Visual | 8599 | 49 | 11 | | 59 | Comunicação Visual | 133 | 49 | 11 | | 60 | Comunicação Visual | 134 | 49 | 10 | +----+----------------------+------------+-----------------+----------+ then this query ... SELECT group_concat(id separator ', ') as ids , disciplina , group_concat(moduloUfcd separator ', ') as mods , idcpDisciplinas , anoTurma FROM gmc GROUP BY idcpDisciplinas, anoTurma; gives ... +--------+----------------------+-----------+-----------------+----------+ | ids | disciplina | mods | idcpDisciplinas | anoTurma | +--------+----------------------+-----------+-----------------+----------+ | 60 | Comunicação Visual | 134 | 49 | 10 | | 58, 59 | Comunicação Visual | 8599, 133 | 49 | 11 | +--------+----------------------+-----------+-----------------+----------+ 1 Quote Link to comment https://forums.phpfreaks.com/topic/311500-mysql-into-to-php-array-issue/#findComment-1581477 Share on other sites More sharing options...
gmc1103 Posted September 18, 2020 Author Share Posted September 18, 2020 Hello Barand How are you? You are the best, great option, it works like i want Best regards Quote Link to comment https://forums.phpfreaks.com/topic/311500-mysql-into-to-php-array-issue/#findComment-1581480 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.