当前位置:网站首页>php中的二维数组去重
php中的二维数组去重
2022-07-02 06:23:00 【夜空の雪風】
前言
有时候在数据库进行join关联查询数据的时候会查询出一些重复的数据需要进行手动去重操作,避免返回到列表中出现重复的数据。
/** * 1.删除二维数组中相同项的数据,一般用于数据库查询结果中相同记录的去重操作 * 2.重置一下二维数组的索引 * @param array $_2d_array 二维数组,类似: * $tmpArr = array( * array('id' => 1, 'value' => '15046f5de5bb708e'), * array('id' => 1, 'value' => '15046f5de5bb708e'), * ); * @param string $unique_key 表示上述数组的 "id" 键,或者 "value" 键 * @return mixed */
function unique_2d_array_by_key($_2d_array, $unique_key)
{
$tmp_key[] = array();
foreach ($_2d_array as $key => &$item) {
if (is_array($item) && isset($item[$unique_key])) {
if (in_array($item[$unique_key], $tmp_key)) {
unset($_2d_array[$key]);
} else {
$tmp_key[] = $item[$unique_key];
}
}
}
//重置一下二维数组的索引
return array_slice($_2d_array, 0, count($_2d_array), false);
}
边栏推荐
- Review of reflection topics
- uniapp引入本地字体
- Latex在VSCODE中编译中文,使用中文路径问题解决
- Apt command reports certificate error certificate verification failed: the certificate is not trusted
- Linux MySQL 5.6.51 Community Generic 安装教程
- js删除字符串的最后一个字符
- Huawei mindspire open source internship machine test questions
- Solve the problem of bindchange event jitter of swiper component of wechat applet
- Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
- 查询GPU时无进程运行,但是显存却被占用了
猜你喜欢
Solution to the black screen of win computer screenshot
Sqli-labs customs clearance (less18-less20)
No process runs when querying GPU, but the video memory is occupied
sqli-labs通关汇总-page1
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
Latex compilation error I found no \bibstyle &\bibdata &\citation command
Linux MySQL 5.6.51 Community Generic 安装教程
Go package name
uniapp引入本地字体
Self study table Au
随机推荐
Promise中有resolve和无resolve的代码执行顺序
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
In depth study of JVM bottom layer (II): hotspot virtual machine object
There are multiple good constructors and room will problem
Deployment API_ automation_ Problems encountered during test
Cve - 2015 - 1635 (ms15 - 034) réplication de la vulnérabilité d'exécution de code à distance
js数组的常用的原型方法
js判断对象是否为空
Latex在VSCODE中编译中文,使用中文路径问题解决
Sqli-labs customs clearance (less15-less17)
Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
Improve user experience defensive programming
Solve the problem of bindchange event jitter of swiper component of wechat applet
Basic knowledge of software testing
如何调试微信内置浏览器应用(企业号、公众号、订阅号)
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
SQLI-LABS通关(less15-less17)
Common function writing method and set get writing method for calculating attributes
sqli-labs通关汇总-page4
UEditor .Net版本任意文件上传漏洞复现