Remove an item from the list of a php array?

Asked

Viewed 211 times

1

PROBLEM:

In the $reserved array the item "09:00" is not being removed with the unset() method inside the foreach.

CODE:

$start = "09:00";  $end = "17:00";   $padding = 60;

$almoco = [ "12:00",  "13:00",  "14:00" ];
$reservado = [ "09:00", "15:00", "17:00", ];

$start_hour_reserve = date_create($start);
$end_hour_reserve = date_create($end);

$horas = date_diff($start_hour_reserve, $end_hour_reserve);

$intervalo = ($horas->h * 60) / $padding;

$appont[] = $start;

for ($i = 1; $i < $intervalo; $i++) {

    $start = date('H:i', strtotime("+{$padding} minutes", strtotime($start)));
    $appont[] = $start;
}

array_push($appont,  $end);


foreach ($almoco as $a) {

    if ($chave = array_search($a, $appont)) {
        unset($appont[$chave]);
    }
}
foreach ($reservado as $r) {

    if ($chave = array_search($r, $appont)) {
        unset($appont[$chave]);
    }
}

return $appont;

1 answer

1


The problem is with this comparison:

if ($chave = array_search($r, $appont))

In this expression PHP will convert the value of $chave for booleano and when converting to boolean, the following values are considered false:

  • the boolean himself false
  • integer 0 (zero)
  • floating point 0.0 (zero)
  • an empty string and the string "0"
  • an array without elements
  • an object with no member variables (PHP 4 only)
  • the special NULL type (including undefined variables) -the Simplexml object created from empty tags

Any other value is considered true.

When the key returned by array_search is equal to 0 , in the case 9:00, zero is converted into false not executing the code inside the if.

To solve should use the strict comparison that in addition to comparing values compares also the types.

    $chave = array_search($a, $appont); 
    if ($chave !== false) { 
        unset($appont[$chave]);
    }

Here $chave(0) is compared to false and are regarded as equal but are not of the same returning type true.

Embedding in your code:

<?php
$start = "09:00";  $end = "17:00";   $padding = 60;

$almoco = [ "12:00",  "13:00",  "14:00" ];
$reservado = [ "09:00", "15:00", "17:00" ];

$start_hour_reserve = date_create($start);
$end_hour_reserve = date_create($end);

$horas = date_diff($start_hour_reserve, $end_hour_reserve);

$intervalo = ($horas->h * 60) / $padding;

$appont[] = $start;

for ($i = 1; $i < $intervalo; $i++) {

    $start = date('H:i', strtotime("+{$padding} minutes", strtotime($start)));
    $appont[] = $start;
}


array_push($appont,  $end);


foreach ($almoco as $a) {
    $chave = array_search($a, $appont); // tira busca de dentro da comparação
    if ($chave !== false) { // Faz uma comparação estrita  
        unset($appont[$chave]);
    }
}


foreach ($reservado as $r) {
    $chave = array_search($r, $appont); // tira busca de dentro da comparação
    if ($chave !== false) { // Faz uma comparação estrita
        unset($appont[$chave]);
    }
}

//No código riginal é return $appont; eu usei o print_r($appont); somente para meus testes
print_r($appont); 
?>

Returning:

Array
(
    [1] => 10:00
    [2] => 11:00
    [7] => 16:00
)

Link to the code

  • 1

    Thank you very much for the explanation, note 10 Auguso

Browser other questions tagged

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