mercredi 27 mai 2020

Remove duplicates in Laravel collection only if previous item is identical

I have a collection of items:

[
    [
    'id' => 1,
    'path' => '/',
    'created_at' => '2020-05-20 20:12:00'
    ],
    [
    'id' => 2,
    'path' => '/somewhere',
    'created_at' => '2020-05-20 21:01:00'
    ],
    [
    'id' => 3,
    'path' => '/somewhere',
    'created_at' => '2020-05-20 21:21:00'
    ],
    [
    'id' => 4,
    'path' => '/somewhere/else',
    'created_at' => '2020-05-20 21:09:00'
    ],
    [
    'id' => 5,
    'path' => '/somewhere/else',
    'created_at' => '2020-05-20 21:10:00'
    ],
]

I need to remove any duplicates where the current item has the same path and is less than 5 minutes old compared to the previous item.

So in this case, item #4 would be removed leaving only #5, but items #2 and #3 would be kept even tho their paths are the same.

How would I do this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire