How to add items to an array correctly?

Asked

Viewed 163 times

1

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 the echos correctly show me what I want to see. However, if just below that is another do:

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

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

I really don’t know the reason for this behavior.

  • The code goal is to create a calendar?

  • Good afternoon.. I am doing a time screen. With the arrays mounted each ABA will have a select with the DIA of the $array_dia variable-array. So when I mount the first tab I pass the $array_dia[0] parameter that would have to contain the day 2017-08-14 the second tab step the $array_dia[1] parameter that would have to contain the day 2017-08-15 and so on :)

2 answers

1


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

Starting with PHP 5, an object variable no longer contains itself object as value. It contains an object identifier that allows that object accessors find the real object.

When you make the modification in the object, through the method below:

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

All reference to it is being changed as well. To avoid this, you can pass the value to another variable, or since you are making this loop repeat for the days. A 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;       
}
  • 1

    Good morning @Marcelo de Andrade... Thank you for your attention. In fact the problem was the OBJECT AND REFERENCE :) With your help I managed to overcome this obstacle. Thanks and hugs :)

  • @Souza Ricardom.If any of the answers helped solve the problem, mark it as solved. If you have questions about, read on: What should I do if someone answers my question?

0

Good morning to all
In fact the problem was what @Marcelo de Andrade said : Objects and reference
My vote goes to him, however what he posted I "THINK" that this with a small mistake of logic. If you do not convert the date to string BEFORE playing into the array, everything remains by reference. So I guess with the code below it all comes together.

$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.:)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.