1

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
        )

)
6
  • 1
    Because you are just replacing the value over and over again til the final loop keep the last value. Commented Nov 28, 2016 at 0:45
  • Well you overwrite the values in your array in each iteration, so you probably want to do something like this: $turmas[]["cod_disciplina"] = $r->cod_disciplina; Commented Nov 28, 2016 at 0:45
  • $turmas is an array, and you overwrite its contents each time through the loop. What do you want it to contain at the end of the loop? Commented Nov 28, 2016 at 0:45
  • @Chris Updated the answer with an example. I expect the whole data inside an array. Because at the end, I need to compare two arrays and detect what is diferent between them. Commented Nov 28, 2016 at 0:49
  • 1
    @Chris Thanks for teaching me this. Already fixed it. Commented Nov 28, 2016 at 1:02

1 Answer 1

2

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;
    }
}  
Sign up to request clarification or add additional context in comments.

3 Comments

Code dumps without any explanation are rarely helpful. Please edit your answer to add some context. Make sure to explain why your code works and the OP's doesn't. (Also, I'd recommend against calling any variable $tmp. That name carries very little meaning. How about $turma?)
It worked, thanks a bunch ! As @Chris said, could you explain me why this $turmas[] = $turma; creates a new array inside $turmas I know it's noob question but I'm kind of new to web develop.
$array[] = ... pushes element to array php.net/manual/en/function.array-push.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.