You can use the array_filter
function to do the calculation. In PHP, even if it is string
it is possible to make the comparisons:
$horas = array_filter($array, function ($hora) use ($inicio, $fim) {
return $hora >= $inicio && $hora <= $fim;
});
This function will create a new array
based on the result of the boolean expression returned by the function. If true, the item is kept in array
. If it is false, it is ignored.
It is important to note that keys are retained when you use array_filter
. If you need to reorder array
, you can call array_values
to solve the problem:
$horas = array_filter($array, function ($hora) use ($inicio, $fim) {
return $hora >= $inicio && $hora <= $fim;
});
$horas = array_values($horas);