我已经制作了一个函数(张贴在底部),它将查看一个多维数组,并删除“部分重复”。我的意思是:

Array
(
    [0] => Array(5,10)
    [1] => Array(5,10,15)
)

第一个数组中的所有元素(5,10)也可以在第二个数组(5,10,15)中使用,因此我们要删除第一个数组,因此我们剩下:

Array(
    [1] => Array(5,10,15)
)

我做出的功能尽管非常慢,但我还是向您转向您,希望您的专业知识能够加快我的功能。

非常感谢您!


$groups =  array(
  array(5,10),
  array(5,10,15)
);
$flag = array();
//clone the groups - we now have two identical arrays with all the groups
$groupsInner = $groups;
//iterate first array of groups
foreach($groups as $index=>$group){
    $flag[]=$index;

    //iterate second array of groups
    foreach($groupsInner as $indexInner=>$groupInner){
        if(!in_array($indexInner, $flag)){

            if(count($group)<count($groupInner)){
                $diff = array_diff($group, $groupInner);
                $array_to_be_deleted = $index;
                $compared_to_array = $indexInner;
                }else{
                $diff = array_diff($groupInner, $group);
                $array_to_be_deleted = $indexInner;
                $compared_to_array = $index;
            }

            //no diff means they are "partly duplicates"
            if(count($diff)==0){
                unset($groups[$array_to_be_deleted]);
                unset($groupsInner[$array_to_be_deleted]);
            }
        }
    }
}
有帮助吗?

解决方案

array_unique(array_merge($first_array,$second_array))

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top