Reputation: 277
I have a 3 levels of multi dimensional arrays and I wanted to check for duplicates and only should return true or false. I tried doing the array_unique()
but I think this one only works for single level array. My array looks like this:
array(
0 => array(
0=> array(
0=> "A"
1=> "B"
2=> "C"
)
1=> array(
0=> "D"
1=> "E"
2=> "F"
)
2=> array(
0=> "G"
1=> "H"
2=> "I"
)
3=> array(
0=> "A"
1=> null
2=> null
)
)
)
the expected result should be that "A" is already duplicated and should return true ,otherwise should be false. Please help. Thanks!
Upvotes: 2
Views: 43
Reputation: 26844
You can flatten your array using RecursiveArrayIterator
and RecursiveIteratorIterator
. To check if there are duplicates, you can check the count of flattened array vs the count unique values of the flattened array.
$arr = //Your array
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach($it as $v) $result[] = $v;
if ( count( $result ) !== count( array_unique( $result ) ) ) {
echo "Duplicate"; //Return true
} else {
echo "No Duplicate";
}
Upvotes: 1