当前位置:网站首页>Illuminate\Support\Collection 去重 unique 列表去重
Illuminate\Support\Collection 去重 unique 列表去重
2022-06-23 04:53:00 【fareast_mzh】
列表, 数组 指定规则去重
curMsg->getFundId() == $prevMsg->getFundId()
重复后处理
// 删除哪个元素?
if ($curMsg->created_at > $prevMsg->created_at) {
// 待删除的元素移动到后面
self::swap($list, $i, $dupIdx);
}
可替换部分如上
没有排序,就这样吧
private static function swap(Collection $c, int $i, int $j) {
$tmp = $c->get($i);
$c->offsetSet($i, $c->get($j));
$c->offsetSet($j, $tmp);
}
/**
* 去除fund_id重复, 相同fund_id取最新的记录,删掉旧的记录
* @param Collection $list
*/
private static function msgListUniq(Collection $list) {
$n = $list->count();
$countDel = 0;
for ($i = 1; $i < $n; $i++) {
$dupIdx = -1;
/** @var $curMsg PayMessage */
$curMsg = $list->get($i);
if (empty($curMsg)) {
break;
}
$prevMsg = null;
for ($j = 0; $j < $i; $j++) {
/** @var $prevMsg PayMessage */
$prevMsg = $list->get($j);
if ($curMsg->getFundId() == $prevMsg->getFundId()) {
$dupIdx = $j;
break;
}
}
// 有重复的fund_id
if (0 <= $dupIdx) {
if ($curMsg->created_at > $prevMsg->created_at) {
self::swap($list, $i, $dupIdx);
}
for ($k = $i+1; $k < $n-$countDel; $k++) {
$list->offsetSet($k-1, $list->get($k));
}
// $list->offsetUnset($k-1);
$countDel += 1;
}
}
// 不要循环中unset
while ($countDel > 0) {
$list->offsetUnset($n - $countDel);
$countDel -= 1;
}
}边栏推荐
- Pat class B 1026 program running time
- Jour 13 Projet de santé mentale - chapitre 13
- Day_07 传智健康项目-Freemarker
- C语言 获取秒、毫秒、微妙、纳秒时间戳
- Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch
- Pat class B 1024 scientific notation C language
- 同步开关电源降低EMI布局 dv/dt di/dt
- 密码学系列之:PKI的证书格式表示X.509
- 机器学习3-岭回归,Lasso,变量选择技术
- [open source project] excel export Lua configuration table tool
猜你喜欢

Summary of ant usage (I): using ant to automatically package apk

Day_12 传智健康项目-JasperReports

Jour 04 projet de santé mentale - gestion des rendez - vous - gestion des forfaits

Ant Usage Summary (II): description of related commands

射频基础理论(dB)

Day_13 传智健康项目-第13章

机器学习3-岭回归,Lasso,变量选择技术

Day_ 12 smart health project jasperreports

Layer 2技术方案进展情况

How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
随机推荐
Given a node of a binary tree, return the successor node of the node
图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?
Day_ 09 smart health project - mobile terminal development - Mobile quick login and permission control
mysql以逗号分隔的字段作为查询条件怎么查——find_in_set()函数
C语言 踩坑:文档编码错误,导致base64中文编码错误
[focus on growth and build a dream for the future] - TDP year-end event, three chapters go to the Spring Festival!
Multiple strings for leetcode topic resolution
In the half year summary, it people just want to lie flat
微软面试题:打印折纸的折痕
Fastdata pole | insight report on e-commerce consumption of young Chinese users 2021
Day_ 11 smart communication health project - graphic report and poi Report
Difference between MySQL read committed and repeatability
Sorting out common problems after crawler deployment
程序员的真实想法 | 每日趣闻
【DaVinci Developer专题】-41-APP SWC如何读取写入NVM Block数据
Memory analysis and memory leak detection
Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library
Implementation of linear list linked list structure
Pyinstaller 打包pyttsx3 出错
Perfect squares for leetcode topic analysis