今天遇到了个需要做二维数组部分字段重复去重的问题,在网上特地找了下,发现以下两种方法是最好用的,特此分享给大家!
二维数组实例:
<?php
$list= [
['name' => '张三','phone' => '123456','time' => "11"]
['name' => '李四','phone' => '345678','time' => "22"]
['name' => '张三','phone' => '123456','time' => "33"]
['name' => '张三','phone' => '123456','time' => "33"]
['name' => '小王','phone' => '345689','time' => "44"]
];
?>
方法一:遍历数组
for($i=0;$i<count($list)-1;$i++){
$source = $list[$i];
foreach($list as $k=>$v){
if($source['name'] == $v['name'] && $k!= $i){
unset($list[$k]);
}
}
}
方法二:递归
function arr($list){
foreach($list $k=>$v){
if($list[0]['name'] == $v['name'] && && $k!=0){
unset($list[$k]);
arr($list);
}
}
return $list;
}
arr($list);
此篇文章由DurkBlue博客申请发布,转载吧请注明来处