删除php中的重叠数组

发布于 2024-10-01 03:30:18 字数 1423 浏览 5 评论 0原文

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

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

I've made a function (posted at the bottom), which will look at a multi-dimensional array, and remove "partly duplicates". What I mean by this is:

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

All the elements in the first array (5, 10), are also available in the second array (5,10,15), and we therefore want to remove the first array so we are left with:

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

The function I've made works, although it is very slow, and I'm therefore turning to you, in the hope that your expertise, may speed up my function.

Thank you very much in advance!


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2024-10-08 03:30:18

array_unique(array_merge($first_array,$second_array))

array_unique(array_merge($first_array,$second_array))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文