如果2个数组重复的值,找出重复的值,在其中一个数组中去除


//array_intersect() 计算数组的交集。 4
//array_intersect_assoc() 比较键名和键值,并返回两个数组的交集数组。 4
//array_intersect_key() 使用键名比较计算数组的交集。 5
//array_intersect_uassoc() 带索引检查计算数组的交集,用回调函数比较索引。 5
//array_intersect_ukey() 用回调函数比较键名来计算数组的交集。



$goods_ids
=array(1=>'12',2=>'15',3=>'16',4=>'18'); $ids=array(4=>'18',5=>'44',6=>'15',7=>'88',); //找出重复的值,这个是忽略键名,是用键值来对比的,如果你需要键值也相同,你可以使用array_intersect_assoc来比较

$rr = array_intersect($goods_ids,$ids);
var_dump($rr); echo '<br>';
//array_search() 在数组中搜索给定的值,如果成功则返回相应的键名
foreach ($rr as $key => $value) { $p =array_search($value,$ids);
//找到键名,并且去除 $ids是被搜索的数组
unset($ids[$p]); echo $p; echo '<br>'; } echo '<br>'; var_dump($ids); echo '<br>';

代码挺简单,主要要把逻辑里清楚
原文地址:https://www.cnblogs.com/zx-admin/p/4665201.html