Bertho Joris Bertho Joris - 4 months ago 8
PHP Question

How to get last element array (remove duplicate data) when I set condition

I have some data in the array will I filter to get the data I want.

In this array I want to take the most recent data in an array which uses certain conditions

I have an array

{

"items": [
{
"idarchive": "1",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-10",
"date2": "2016-07-10",
"period": "1",
"ts_archived": "2016-07-10 17:32:55",
"value": "10"
},
{
"idarchive": "2",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-11",
"date2": "2016-07-11",
"period": "1",
"ts_archived": "2016-07-10 17:32:56",
"value": "30"
},
{
"idarchive": "3",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-11",
"date2": "2016-07-11",
"period": "1",
"ts_archived": "2016-07-10 18:11:32",
"value": "50"
},
{
"idarchive": "4",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-12",
"date2": "2016-07-12",
"period": "1",
"ts_archived": "2016-07-11 10:02:44",
"value": "15"
},
{
"idarchive": "5",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-12",
"date2": "2016-07-12",
"period": "1",
"ts_archived": "2016-07-11 11:12:05",
"value": "20"
},
]
}


In my array data, I have a date field 1 is the key for me to do filter.

Inside the array, I want to get the
last array
for the ``.
They assured the data will I get to be like this

{

"items": [
{
"idarchive": "1",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-10",
"date2": "2016-07-10",
"period": "1",
"ts_archived": "2016-07-10 17:32:55",
"value": "10"
},
{
"idarchive": "3",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-11",
"date2": "2016-07-11",
"period": "1",
"ts_archived": "2016-07-10 18:11:32",
"value": "50"
},
{
"idarchive": "5",
"name": "nb_uniq_visitors",
"idsite": "1",
"date1": "2016-07-12",
"date2": "2016-07-12",
"period": "1",
"ts_archived": "2016-07-11 11:12:05",
"value": "20"
},
]
}


One data represent all data the same date.

I only can do a filter against the same data but have not reached a certain condition. This is my code

$output = array();
array_walk_recursive($myArrayDataAbove, function($item, $key) use (&$output){
$output[$key] = isset($output[$key]) ? $item + $output[$key] : $item;
});
echo '<pre>';
var_dump($output);


Thanks

Answer

So your items are in a variable named $items.

Create a second variable where you are going to store the last results :

$lastItems = [];

Loop through your items to check the dates :

foreach ($items as $item) {
    // get the date
    $itemDate = $item['date1'];

    // if the date does not exist in your lastItems array, just create it :
    if (!isset($lastItems[$itemDate])) {
        $lastIems[$itemDate] = $item;
    } else {
        // the date exists, lets compare it to the item :
        $itemArchivedDate = new \DateTime($item['ts_archived']);
        $lastArchivedDate = new \DateTime($lastItems[$itemDate]['ts_archived']);

        // only if the current item archive date is greater than the stored one, replace it :
        if ($itemArchivedDate > lastArchivedDate) {
            $lastIems[$itemDate] = $item;
        }
    }
}

There you go, your last elements are stored in the $lastItems variable.