$c = [
["aid" => 8, "did" => 1],
["aid" => 2, "did" => 3],
["aid" => 3, "did" => 4],
["aid" => 5, "did" => 5],
["aid" => 2, "did" => 6],
["aid" => 2, "did" => 9],
];
public function retainLargestDid($array) {
$maxDids = [];
$filteredArray = [];
foreach ($array as $item) {
$aid = $item['aid'];
$did = $item['did'];
// 检查是否已经有这个aid的记录,如果没有或者找到了更大的did,则更新记录
if (!isset($maxDids[$aid]) || $did > $maxDids[$aid]) {
$maxDids[$aid] = $did;
}
}
// 遍历原始数组,只保留具有最大did的元素
foreach ($array as $item) {
if ($item['did'] === $maxDids[$item['aid']]) {
$filteredArray[] = $item;
}
}
return $filteredArray;
}
若aid相等则保留最大的did的那个数组,其余的删除,且保留原有的排序 PHP
发表评论