当前位置:网站首页>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);
}
边栏推荐
- 2020-9-23 use of QT timer qtimer class.
- Basic knowledge of software testing
- 【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
- Go common compilation fails
- js的防抖和节流
- UEditor .Net版本任意文件上传漏洞复现
- Redis -- cache breakdown, penetration, avalanche
- Cve - 2015 - 1635 (ms15 - 034) réplication de la vulnérabilité d'exécution de code à distance
- Anti shake and throttling of JS
- VSCODE 安装LATEX环境,参数配置,常见问题解决
猜你喜欢
SQLI-LABS通关(less6-less14)
js中map和forEach的用法
Sqli labs customs clearance summary-page4
unittest. Texttestrunner does not generate TXT test reports
SQLI-LABS通关(less18-less20)
Review of reflection topics
sqli-labs通关汇总-page2
flex九宫格布局
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
Win10: add or delete boot items, and add user-defined boot files to boot items
随机推荐
Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
table 组件指定列合并行方法
JS divides an array into groups of three
Go common compilation fails
js中map和forEach的用法
Win电脑截图黑屏解决办法
js数组的常用的原型方法
Sqli-labs customs clearance (less18-less20)
The table component specifies the concatenation parallel method
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现
js判断对象是否为空
Code execution sequence with and without resolve in promise
Blog directory of zzq -- updated on 20210601
JS countdown case
CAD二次开发 对象
js判断数组中对象是否存在某个值
Implement strstr() II
SQLI-LABS通关(less18-less20)
查询GPU时无进程运行,但是显存却被占用了
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command