Unset element in array nested, between a time interval

Viewed 38

I'm trying to remove an element.

I've a reservation with:

$StartTime = "14:00"; $EndTime = "16:30";

and Array with

Array
(
    [Tennis 1] => Array
        (
            [IntervalTime] => Array
                (
                    [Lun] => Array
                        (
                            [0] => 10:00
                            [1] => 11:00
                            [2] => 12:00
                            [3] => 13:00
                            [4] => 14:00
                            [5] => 15:00
                            [6] => 16:00
                            [7] => 17:00
                            [8] => 18:00
                            [9] => 19:00
                            [10] => 20:00
                        )

                    [Mar] => Array
                        (
                            [0] => 12:00
                            [1] => 13:00
                            [2] => 14:00
                            [3] => 15:00
                        )

                    [Mer] => Array
                        (
                            [0] => 09:00
                            [1] => 10:00
                            [2] => 11:00
                            [3] => 12:00
                            [4] => 13:00
                        )

                    [Gio] => Array
                        (
                            [0] => 09:00
                            [1] => 10:00
                            [2] => 11:00
                            [3] => 12:00
                            [4] => 13:00
                        )

                    [Ven] => Array
                        (
                            [0] => 09:00
                            [1] => 10:00
                            [2] => 11:00
                            [3] => 12:00
                            [4] => 13:00
                        )

                    [Sab] => Array
                        (
                            [0] => 09:00
                            [1] => 10:00
                            [2] => 11:00
                            [3] => 12:00
                            [4] => 13:00
                            [5] => 14:00
                            [6] => 15:00
                            [7] => 16:00
                            [8] => 17:00
                            [9] => 18:00
                        )

                    [Sun] => Array
                        (
                            [0] => 09:00
                            [1] => 10:00
                            [2] => 11:00
                            [3] => 12:00
                            [4] => 13:00
                            [5] => 14:00
                            [6] => 15:00
                            [7] => 16:00
                            [8] => 17:00
                            [9] => 18:00
                        )

                )

        )

I need to remove element between $StartTime and $EndTime
so for "Lun" array need to be removed:

                            [0] => 10:00
                            [1] => 11:00
                            [2] => 12:00
                            [3] => 13:00
                            [4] => 14:00 // need remove
                            [5] => 15:00 // need remove
                            [6] => 16:00 // need remove
                            [7] => 17:00
                            [8] => 18:00
                            [9] => 19:00
                            [10] => 20:00

I would to foearch the time from Start to End date, and then remove with:

unset($DisponibilitaRoom[$Nomeroom]["IntervalTime"]["Lun"][$index]);
2 Answers

You correct - simple unset should do it - but you need to loop on the times and remove them as:

foreach($arr['Tennis 1']['IntervalTime']['Lun'] as $k => $v) {
    if ($v >= $StartTime && $v < $EndTime)
        unset($arr['Tennis 1']['IntervalTime']['Lun'][$k]);
}

Live example: 3v4l

Or you can use array-filter as

$arr['Tennis 1']['IntervalTime']['Lun'] = array_filter($arr['Tennis 1']['IntervalTime']['Lun'], function ($v) use ($StartTime, $EndTime) {
    return !($v >= $StartTime && $v < $EndTime);
} );

You can use array_walk_recursive with array_map and array_filter

array_walk_recursive($a['Tennis 1']['IntervalTime'], function(&$v) use ($StartTime, $EndTime){
    if($v >= $StartTime && $v <= $EndTime) $v = false;
});
$f['Tennis 1']['IntervalTime'] = array_map('array_filter', $a['Tennis 1']['IntervalTime']);
print_r($f);

Working example :- https://3v4l.org/4tNEM

Related