Post conditional strange behavior (creating array)

Asked

Viewed 23 times

0

Whereas I have these auxiliary classes:

class HorasDisponiveis {
    public $diaSemana;
    public $horario;
    public $disponivel;
    public $confirmado;
    public $qtdDocas;
}

class FaixasDisponiveis {
    public $horaInicial;
    public $horaFinal;
    public $intervalo;
    public $livre;
    public $data;
    public $diaSemana;
}

Plus the code:

$faixasDisponiveis = [];

        foreach($horasDisponiveis as $item){
            if(!isset($horaInicial)) {
                $horaInicial = str_pad($item->horario->hour, 2, 0, STR_PAD_LEFT) . ":" . str_pad($item->horario->minute, 2, 0, STR_PAD_LEFT);
            }

            $horaFinal = str_pad($item->horario->hour, 2, 0, STR_PAD_LEFT) . ":" . str_pad($item->horario->minute, 2, 0, STR_PAD_LEFT);

            if($item->disponivel){
                $faixaDisponivel = new FaixasDisponiveis();
                $faixaDisponivel->horaInicial = $horaInicial;
                $faixaDisponivel->horaFinal = $horaFinal;
                $faixaDisponivel->diaSemana = $item->horario->dayOfWeek;
                $faixaDisponivel->data = $item->horario->year . "-" . $item->horario->month . "-" . $item->horario->day;
                array_push($faixasDisponiveis, $faixaDisponivel);
                unset($horaFinal);
                unset($horaInicial);
            }
        }
        return $faixasDisponiveis;

I have the following result:

 "data": [
         {
             "horaInicial": [
                 "10",
                 "00"
             ],
             "horaFinal": "07:00",
             "intervalo": null,
             "livre": null,
             "data": "2019-5-16",
             "diaSemana": 4
         }, (...)

My question is, why does it transform the variable into an array by inserting the setted conditioner (isset)? Note that the only difference between the $hourly variable and the $hourly variable is just checking if it is set.

Data ($hoursAvailable)

"data": [
        {
            "diaSemana": 4,
            "horario": {
                "date": "2019-05-16 07:00:00.000000",
                "timezone_type": 3,
                "timezone": "America/Sao_Paulo"
            },
            "disponivel": true,
            "confirmado": false,
            "qtdDocas": 2
        },
        {
            "diaSemana": 4,
            "horario": {
                "date": "2019-05-16 07:10:00.000000",
                "timezone_type": 3,
                "timezone": "America/Sao_Paulo"
            },
            "disponivel": true,
            "confirmado": false,
            "qtdDocas": 2
        },
        {
            "diaSemana": 4,
            "horario": {
                "date": "2019-05-16 07:20:00.000000",
                "timezone_type": 3,
                "timezone": "America/Sao_Paulo"
            },
            "disponivel": true,
            "confirmado": false,
            "qtdDocas": 2
        }(...)
  • I believe the problem is not facing isset, it does not manipulate the variable, in the documentation does not indicate anything of the type and I did tests here and nothing was changed. Makes a test and puts an isset in $horaFinal to check if there is any relationship.

  • You can give an example of the values of your array $horasDisponiveis? I could not reproduce this problem in the tests I did https://ideone.com/LJvB1Y.

  • I edited the question and added the value.

  • Adding isset to $horaFinal, the behavior was the same, transformed into an array. My question isn’t even the value itself, but the reason why you’re turning the string into an array, this is the centerpoint.

  • 1

    I have already identified the error. In one section above in the code, there are within another foreach two variables with the same name. In my idea, these variables would fall within the scope of foreach, which does not occur in php by the looks of it. I set both to null and the error no longer occurred.

  • Try adding a condition to the if: if(!isset($horaInicial) or true). If the error disappears, the problem may be the format of the variable before going through if

Show 1 more comment
No answers

Browser other questions tagged

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