PHP two-dimensional array deduplication problem advice, is there a built-in function?

data format:

$data = [
    [
        "id"=>"a8856",
        "date"=>"20100612"
    ],
    [
        "id"=>"a8856",
        "date"=>"20180102"
    ],
    [
        "id"=>"top856",
        "date"=>"20100612"
    ],
    [
        "id"=>"c8236",
        "date"=>"20100612"
    ],
    [
        "id"=>"e2569",
        "date"=>"20010612"
    ],
    [
        "id"=>"e2569",
        "date"=>"20150825"
    ]
];

ask for help: the id value in the above array may be duplicated, but the date value is unique. I want to go to multiple entries to keep the latest record, for example: 2 id are all e2569, but one date is 2001 and the other is 2015, delete 2001 and keep 2015, there may be multiple duplicate id values.

is there anything that can solve this problem, such as online, demo, thank you, or built-in functions?

Php
Jun.10,2022

foreach($data as $v){
    if(!isset($arr[$v['id']])){
        $arr[$v['id']] = $v;
    }elseif($arr[$v['id']]['date'] < $v['date']){
        $arr[$v['id']]['date'] = $v['date'];
    }
}
$new = array_values($arr);

compare
usort ($data,function ($A1 maxia2) {return $A1 ['date'] > $a2 [' date'];});
$result=array_combine (array_column ($data,'id'), array_column ($data,'date'));
$result=array_map ($mPerformn) {return ['id'= > $mPerformance = > $n];}, array_keys ($result), array_values ($result);
var_dump ($result)


array_column ($data,'data','id')


if you need top-down coverage, just array_column ($data,NULL,'id'), so that the same id value will be subject to the last ending


if you want to remove multiple entries and keep the latest date record, then compare in a loop.
built-in function can only repeat. Cannot keep date latest record

Menu