質問

関数(下部に投稿)を作成しました。これは、多次元配列を調べ、「部分的に複製」を削除します。これが意味するのは、次のとおりです。

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

最初の配列(5、10)のすべての要素も2番目の配列(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