How to add items to an array correctly?

2

I'm trying to create arrays with days of the week. "Apparently" seems to work:

$array_dia_da_semana = array("DOMINGO","SEGUNDA","TERÇA","QUARTA","QUINTA","SEXTA","SÁBADO");

$array_dia_da_semana_d = [];
$array_dia_da_semana_s = [];
$array_dia             = [];

$data = new DateTime('2017-08-14');     // Pega a data de hoje
for ($a=1; $a <=7; $a++)
{   
    $array_dia[$a - 1] = $data;
    echo(date_format($array_dia[$a - 1],"Y-m-d") . "  iiii<br>");

    $diaN = date( "w", strtotime($data->format('Y-m-d')));
    echo("dia em numero.. " . $diaN) . "<br> ";
    $data->modify('+1 day');

    $array_dia_da_semana_s[] = $array_dia_da_semana[$diaN];
    echo($array_dia_da_semana_s[$a-1] . "<br>");        
    $array_dia_da_semana_d[] = $diaN;       
    echo($array_dia_da_semana_d[$a-1] . "<br>");    
}

All echos shows me correctly what I want to see. However, if below this is to do another for:

for ($a=0; $a <=6; $a++)
{   
    echo(date_format($array_dia[$a],"Y-m-d") . "  <br>");
}

What I see is that all items in the array have same date . Which in this case is the last day manipulated "2017-08-21".

I really do not know why.

    
asked by anonymous 14.08.2017 / 18:57

2 answers

1

This behavior is described in the Objects and References section of the documentation of PHP , it says:

  

As of PHP 5, an object variable no longer contains itself   object as value. It contains an object identifier that allows   that object accessors find the actual object.

When you modify the object, using the method below:

$data->modify("+1 day");

The entire reference to it is being changed as well. To avoid this, you can pass the value to another variable, or since you are doing this loop repetition for days. One possible solution would be:

for ($a=1; $a <=7; $a++) {   
    $data = new DateTime('2017-08-14');     // Pega a data de hoje
    $array_dia[$a - 1] = $data;
    $diaN = date( "w", strtotime($data->format('Y-m-d')));
    $data->modify("+{$a} day");

    $array_dia_da_semana_s[] = $array_dia_da_semana[$diaN];
    $array_dia_da_semana_d[] = $diaN;       
}

Using date :

$data = date('Y-m-d');   
for ($a=1; $a <=7; $a++) {   

    $array_dia[$a - 1] = $data;
    $diaN = date( "w", strtotime($data));
    $data = date('Y-m-d', strtotime("{$data} +1 day"));

    $array_dia_da_semana_s[] = $array_dia_da_semana[$diaN];
    $array_dia_da_semana_d[] = $diaN;       
}
    
15.08.2017 / 04:06
0

Good morning everyone
In fact the problem was what @Marcelo de Andrade spoke: Objects and reference
My vote goes to him, however what he posted I "ACHO" that this with a small error of logic. If you do not convert the date to string BEFORE playing into the array, everything remains by reference. So I think with the code below everything is resolved.

$array_dia_da_semana = array("DOMINGO","SEGUNDA","TERÇA","QUARTA","QUINTA","SEXTA","SÁBADO");

$array_dia_da_semana_d = [];
$array_dia_da_semana_s = [];
$array_dia             = [];

$data = new DateTime('2017-08-14');     // Pega a data de hoje
//$data =  new DateTime($_POST['pdata']);
for ($a=1; $a <=7; $a++)
{   
    $array_dia[] = date_format($data,"Y-m-d"); // Unica linha alterada. converter a data para string.
    //echo(date_format($array_dia[$a - 1],"Y-m-d") . "  iiii<br>");

    $diaN = date( "w", strtotime($data->format('Y-m-d')));
    //echo("dia em numero.. " . $diaN) . "<br> ";
    $data->modify('+1 day');



    $array_dia_da_semana_s[] = $array_dia_da_semana[$diaN];
    //echo($array_dia_da_semana_s[$a-1] . "<br>");      
    $array_dia_da_semana_d[] = $diaN;       
    //echo($array_dia_da_semana_d[$a-1] . "<br>");  
}


for ($a=0; $a <=6; $a++)
{   
    echo($array_dia[$a]."  <br>");
    echo($array_dia_da_semana_s[$a]."  <br>");
    echo($array_dia_da_semana_d[$a]."  <br>");
}

I just have to thank @Marcelo de Andrade for sharing his knowledge with us :) Thanks, and hugs to all.:)

    
15.08.2017 / 16:31