简体   繁体   中英

Feeding associative array through loop

I'm trying to feed an associative array but it always keeps only the last inserted value.

$turmas = array(
        'cod_disciplina' => '', 
        'cod_turma'      => '', 
        'hr_inicio'      => '', 
        'hr_fim'         => '', 
        'dia_semana'     => ''
        );

foreach($temp as $t)
        {
           $result = Horario::select('cod_disciplina', 'cod_turma', 'hr_inicio', 'hr_fim', 'dia_semana')
            ->where('cod_disciplina', $t->cod_disciplina)
            ->where('cod_turma', $t->cod_turma)
            ->where('ano_semestre', $ano_semestre)->get();

            foreach($result as $r)
            {
                $turmas['cod_disciplina'] = $r->cod_disciplina;
                $turmas['cod_turma'] = $r->cod_turma;
                $turmas['hr_inicio'] = $r->hr_inicio;
                $turmas['hr_fim']    = $r->hr_fim;
                $turmas['dia_semana']    = $r->dia_semana;

            }            
        }  

The array $turmas always keep the last insert only. I know it's overwriting things, but I have no idea how to fix it.

This is an example of what I expect as a final result.

Array
(
    [0] => Array
        (
            [cod_disciplina] => DPS1000
            [cod_turma] => 11
            [hr_inicio] => 15:30:00
            [hr_fim] => 17:30:00
            [dia_semana] => Terça-feira
        )

    [1] => Array
        (
            [cod_disciplina] => DPS1000
            [cod_turma] => 11
            [hr_inicio] => 13:30:00
            [hr_fim] => 15:30:00
            [dia_semana] => Quarta-feira
        )

    [2] => Array
        (
            [cod_disciplina] => DPS1002
            [cod_turma] => 10
            [hr_inicio] => 08:30:00
            [hr_fim] => 12:30:00
            [dia_semana] => Quarta-feira
        )

    [3] => Array
        (
            [cod_disciplina] => DPS1003
            [cod_turma] => 10
            [hr_inicio] => 07:30:00
            [hr_fim] => 09:30:00
            [dia_semana] => Segunda-feira
        )

    [4] => Array
        (
            [cod_disciplina] => DPS1003
            [cod_turma] => 10
            [hr_inicio] => 10:30:00
            [hr_fim] => 10:30:00
            [dia_semana] => Segunda-feira
        )

)

What you want is an array of arrays, so inside your foreach append new $turma array to $turmas array.

$turmas = [];

foreach($temp as $t) {
    $result = Horario::select('cod_disciplina', 'cod_turma', 'hr_inicio', 'hr_fim', 'dia_semana')
    ->where('cod_disciplina', $t->cod_disciplina)
    ->where('cod_turma', $t->cod_turma)
    ->where('ano_semestre', $ano_semestre)->get();

    foreach($result as $r) {
        $turma = [];
        $turma['cod_disciplina'] = $r->cod_disciplina;
        $turma['cod_turma'] = $r->cod_turma;
        $turma['hr_inicio'] = $r->hr_inicio;
        $turma['hr_fim'] = $r->hr_fim;
        $turma['dia_semana'] = $r->dia_semana;
        $turmas[] = $turma;
    }
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM