已经发布了许多关于从多维数组中删除重复项或特定值的问题,但我似乎无法找到任何关于将多维数组与自身进行比较并根据数组中的值删除valuse的内容。
在下面的示例中,我想删除其中反转array [0]和array [2]的任何数组。因此,在下面的示例中,对于数组[0](A,B)和[3](B,A),[3]将被删除。对于阵列[1](B,C)和[2](C,B),[2]将被删除。
Array (
[0] => Array ( [0] => A [1] => X [2] => B )
[1] => Array ( [0] => C [1] => Y [2] => B )
[2] => Array ( [0] => B [1] => V [2] => C )
[3] => Array ( [0] => A [1] => Z [2] => A )
[4] => Array ( [0] => B [1] => W [2] => A )
[5] => Array ( [0] => B [1] => V [2] => B ))
结果数组应为
Array (
[0] => Array ( [0] => A [1] => X [2] => B )
[1] => Array ( [0] => C [1] => Y [2] => B )
[2] => Array ( [0] => A [1] => Z [2] => A )
[3] => Array ( [0] => B [1] => V [2] => B ))
答案 0 :(得分:0)
一种方法是创建一个临时容器来检查它是否存在,然后从那里过滤它:
$temp = []; // temporary container
$new_arr = array_filter($arr, function($e) use(&$temp) {
$orig = $e; // create an original copy
sort($e); // sort
if(!in_array($e, $temp)) {
$temp[] = $e; // push if doesn't exist yet
return $orig; // return the orig
}
});
答案 1 :(得分:0)
遍历该数组并检查每个元素是否与其他元素相对应,如果满足条件,则取消设置其他元素。
$array = [
['A', 'X', 'B'],
['C', 'Y', 'B'],
['B', 'V', 'C'],
['A', 'Z', 'A'],
['B', 'W', 'A'],
['B', 'V', 'B'],
];
array_walk($array, function (array $a1, int $k1) use (&$array) {
foreach ($array as $k2 => $a2) {
if ($k1 !== $k2 and $a1[0] === $a2[2] and $a1[2] === $a2[0]) {
unset($array[$k2]);
}
}
});