PHP - How to test a multidimensional array for repeating element values ββin any order
I'm not sure if the title really touches on what I'm asking, so here's what I'm trying to do:
I have an array of arrays with 4 integer elements i.e.
Array(Array(1,2,3,4), Array(4,2,3,1), Array(18,3,22,9), Array(23, 12, 33, 55))
I basically need to delete one of the two arrays that have the same values ββin any order, like indices 0 and 1 in the above example.
I can do this quite easily when only checking two elements using the best answer code in this question .
My multidimensional array can have 1-10 arrays at any given time, so I cannot find an optimal way to handle such a structure and delete the arrays with the same numbers in any order.
Many thanks!
source to share
I thought about this, and I think using a well-designed closure witharray_filter
might be the way I might go about it:
$matches = array();
$array = array_filter($array, function($ar) use (&$matches) {
sort($ar);
if(in_array($ar, $matches)) {
return false;
}
$matches[] = $ar;
return true;
});
See an example here: http://ideone.com/Zl7tlR
Edit: $array
will be your final result, ignore $matches
how it was used during filter closure.
source to share