1

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

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]);
            }
        }
    }
}
4

1 回答 1

1

array_unique(array_merge($first_array,$second_array))

于 2010-11-06T22:14:18.327 回答