Abnormal foreach behavior in php 7.0, repeat the last element in nested foreach

1

I'm new here, and as the title says the foreach behaves abnormally. I want to list all the driver's schedules per shift (1 = day, 2 = night), in turn these with the days from Monday to Friday (0 = Sunday, 1 = Monday, etc) Bearing in mind that driver 1 has only scheduled turn 2 from Monday to Friday

Here is my table: TBL_CONDUCTOR_HORARIO


id  dia turno  fecha_reg  conductor_id
1    0  2    2017-12-11      1
2    2  2    2017-12-11      1
3    4  2    2017-12-11      1
4    6  2    2017-12-11      1
5    1  2    2017-12-11      1
6    3  2    2017-12-11      1
7    5  2    2017-12-11      1

Here my code:

$turnos[] = (object)['id'=>TURNO_DIA,'title'=>'Día'];   
$turnos[] = (object)['id'=>TURNO_NOCHE,'title'=>'Noche'];
$dias = [
       (object)['id'=>'0', 'name'=>'Domingo'],
       (object)['id'=>'1', 'name'=>'Lunes'],
       (object)['id'=>'2', 'name'=>'Mártes'],
       (object)['id'=>'3', 'name'=>'Miércoles'],
       (object)['id'=>'4', 'name'=>'Jueves'],
       (object)['id'=>'5', 'name'=>'Viernes'],
       (object)['id'=>'6', 'name'=>'Sábado']
       ];
foreach($turnos as $c=>$turno)
  {
    foreach($dias as $dia)
    {
       $rsHorarioTurno = (new Select())
       ->from(array('tu'=>TBL_CONDUCTOR_HORARIO))
       ->where('tu.condh_conductor_cond_id = ?', $idConductor)
       ->where('tu.condh_dia = ?', $dia->id)
       ->where('tu.condh_turno = ?', $turno->id)
       ->fetchRow();

      if($rsHorarioTurno)
      {
        $dia->checked = "checked";
      }else{
        $dia->checked = "";
      }
    }

    $turno->dias = $dias;
  }

var_dump($turnos);

Here is the problem, the days that add that if they are marked or not, they always come out the same, either turn 1 or turn 2.

And so it prints me:

array(2) {
  [0]=>
  object(stdClass)#34 (3) {
    ["id"]=>
    int(1)
    ["title"]=>
    string(4) "Día"
    ["dias"]=>
    array(7) {
      [0]=>
      object(stdClass)#26 (3) {
        ["id"]=>
        string(1) "0"
        ["name"]=>
        string(7) "Domingo"
        ["checked"]=>
        string(7) "checked"
      }
      [1]=>
      object(stdClass)#27 (3) {
        ["id"]=>
        string(1) "1"
        ["name"]=>
        string(5) "Lunes"
        ["checked"]=>
        string(7) "checked"
      }
      [2]=>
      object(stdClass)#29 (3) {
        ["id"]=>
        string(1) "2"
        ["name"]=>
        string(7) "Mártes"
        ["checked"]=>
        string(7) "checked"
      }
      [3]=>
      object(stdClass)#30 (3) {
        ["id"]=>
        string(1) "3"
        ["name"]=>
        string(10) "Miércoles"
        ["checked"]=>
        string(7) "checked"
      }
      [4]=>
      object(stdClass)#31 (3) {
        ["id"]=>
        string(1) "4"
        ["name"]=>
        string(6) "Jueves"
        ["checked"]=>
        string(7) "checked"
      }
      [5]=>
      object(stdClass)#32 (3) {
        ["id"]=>
        string(1) "5"
        ["name"]=>
        string(7) "Viernes"
        ["checked"]=>
        string(7) "checked"
      }
      [6]=>
      object(stdClass)#33 (3) {
        ["id"]=>
        string(1) "6"
        ["name"]=>
        string(7) "Sábado"
        ["checked"]=>
        string(7) "checked"
      }
    }
  }
  [1]=>
  object(stdClass)#35 (3) {
    ["id"]=>
    int(2)
    ["title"]=>
    string(5) "Noche"
    ["dias"]=>
    array(7) {
      [0]=>
      object(stdClass)#26 (3) {
        ["id"]=>
        string(1) "0"
        ["name"]=>
        string(7) "Domingo"
        ["checked"]=>
        string(7) "checked"
      }
      [1]=>
      object(stdClass)#27 (3) {
        ["id"]=>
        string(1) "1"
        ["name"]=>
        string(5) "Lunes"
        ["checked"]=>
        string(7) "checked"
      }
      [2]=>
      object(stdClass)#29 (3) {
        ["id"]=>
        string(1) "2"
        ["name"]=>
        string(7) "Mártes"
        ["checked"]=>
        string(7) "checked"
      }
      [3]=>
      object(stdClass)#30 (3) {
        ["id"]=>
        string(1) "3"
        ["name"]=>
        string(10) "Miércoles"
        ["checked"]=>
        string(7) "checked"
      }
      [4]=>
      object(stdClass)#31 (3) {
        ["id"]=>
        string(1) "4"
        ["name"]=>
        string(6) "Jueves"
        ["checked"]=>
        string(7) "checked"
      }
      [5]=>
      object(stdClass)#32 (3) {
        ["id"]=>
        string(1) "5"
        ["name"]=>
        string(7) "Viernes"
        ["checked"]=>
        string(7) "checked"
      }
      [6]=>
      object(stdClass)#33 (3) {
        ["id"]=>
        string(1) "6"
        ["name"]=>
        string(7) "Sábado"
        ["checked"]=>
        string(7) "checked"
      }
    }
  }
}

As you can see in both are equal. Thanks for the help in advance.

    
asked by Lenin 11.12.2017 в 22:46
source

2 answers

1

$turno->dias = $dias; assigns to $turno->dias the value $dias that is an object. Each subsequent modification to $dias affects all the elements that referred to that object. That is, when you change the value of $dias for the night shift, it also changes the $dias object that you had assigned to the day shift.

This is in the documentation :

  

As of PHP 5, an object variable does not contain the object itself   value anymore It only contains an object identifier that allows   object accessors to find the current object. When an object is sent by   argument, returned or assigned to another variable, the different   variables are not aliases: they hold a copy of the identifier, which    points to the same object .

You should do $turno->dias = clone $dias;

    
answered by 12.12.2017 в 13:57
1

On the second day of the presentation the problem was solved, here I leave as I did:

foreach($turnos as $c=>$turno)
  {
    foreach($dias as $dia)
    {
       $rsHorarioTurno = (new Select())
       ->from(array('tu'=>TBL_CONDUCTOR_HORARIO))
       ->where('tu.condh_conductor_cond_id = ?', $idConductor)
       ->where('tu.condh_dia = ?', $dia->id)
       ->where('tu.condh_turno = ?', $turno->id)
       ->fetchRow();

      if($rsHorarioTurno)
      {
        $dia->checked = "checked";
      }else{
        $dia->checked = "";
      }
    }
    $tmp = serialize($dias);
    $turno->dias = unserialize($tmp);

  }

what I added was the

$tmp = serialize($dias) ;
serialize and then the
unserialize($tmp);
unserialize thanks to @amenadiel that I passed the documentation and I could find this function.     
answered by 12.12.2017 в 17:19