إزالة المصفوفات المتداخلة في PHP
-
29-09-2019 - |
سؤال
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))
لا تنتمي إلى StackOverflow