Filter dates in a nested php array-ThrowExceptions

Exception or error:

I have this nested array, it’s in a variable called $dates_list

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [event_date] => 2020-11-19 00:00:00
                    [event_start_time] => 13:07:00
                    [event_end_time] => 17:07:00
                )

            [1] => Array
                (
                    [event_date] => 2020-06-17 00:00:00
                    [event_start_time] => 10:07:00
                    [event_end_time] => 17:07:00
                )

            [2] => Array
                (
                    [event_date] => 2020-03-05 00:00:00
                    [event_start_time] => 15:46:00
                    [event_end_time] => 20:46:00
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [event_date] => 2020-07-07 00:00:00
                    [event_start_time] => 20:10:00
                    [event_end_time] => 20:10:00
                )

            [1] => Array
                (
                    [event_date] => 2020-03-13 00:00:00
                    [event_start_time] => 20:10:00
                    [event_end_time] => 20:10:00
                )

        )

)

and I’ve got a variable called $now

$now = date("Y-m-d h:i:s"); 
// 2020-03-13 02:28:32

How do I filter out all the event_date keys that are older than $now?

If event_date is the same as $now I’d like that to be left in the array

Desired result

I’m hoping to be able to get it to look like this, notice [event_date] => 2020-03-05 00:00:00 has been removed

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [event_date] => 2020-11-19 00:00:00
                    [event_start_time] => 13:07:00
                    [event_end_time] => 17:07:00
                )

            [1] => Array
                (
                    [event_date] => 2020-06-17 00:00:00
                    [event_start_time] => 10:07:00
                    [event_end_time] => 17:07:00
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [event_date] => 2020-07-07 00:00:00
                    [event_start_time] => 20:10:00
                    [event_end_time] => 20:10:00
                )

            [1] => Array
                (
                    [event_date] => 2020-03-13 00:00:00
                    [event_start_time] => 20:10:00
                    [event_end_time] => 20:10:00
                )

        )

)
How to solve:

You have to loop all items in Array and then compare dates:

foreach($dates_list as $key=>$item) {
    foreach($item as $k=>$value) {
        if (date("Y-m-d h:i:s", strtotime($value["event_date"])) < $now) {
             unset($dates_list[$key][$k]);
        }
    }
}

Answer´╝Ü

You could use the array_filter_recursive() suggested in this user comment and just recurisvely filter through the arrays:

function array_filter_recursive($input, $callback = null)
{
    foreach ($input as &$value) {
        if (is_array($value)) {
            $value = array_filter_recursive($value, $callback);
        }
    }
    return array_filter($input, $callback);
}

$array = [
    [
        ['event_date' => '2020-11-19 00:00:00'],
        ['event_date' => '2020-06-17 00:00:00'],
        ['event_date' => '2020-03-05 00:00:00'],
    ],
    [
        ['event_date' => '2020-07-07 00:00:00'],
        ['event_date' => '2020-03-13 00:00:00'],
    ]
];
$currentTime = '2020-03-13 02:28:32'; // date and time from question

$filtered = array_filter_recursive($array, function($value) use ($currentTime) {
    // do not remove the first-level arrays
    if (!isset($value['event_date'])) {
        return true;
    }
    return $value['event_date'] > $currentTime;
});

var_export($filtered);

output:

array (
  0 =>
  array (
    0 =>
    array (
      'event_date' => '2020-11-19 00:00:00',
    ),
    1 =>
    array (
      'event_date' => '2020-06-17 00:00:00',
    ),
  ),
  1 =>
  array (
    0 =>
    array (
      'event_date' => '2020-07-07 00:00:00',
    ),
  ),
)

Leave a Reply

Your email address will not be published. Required fields are marked *