array_unique for arrays inside array

Yosef picture Yosef · Mar 6, 2011 · Viewed 10.5k times · Source

I need a function like array_unique for arrays inside array.

The Case - should be equal, but output "not equal":

<?php
$arr=array(array('a',1),array('a',2));
$arr2=array_unique($arr);
if($arr2==$arr){
  echo "equal";
}
else{
  echo "not equal";
}
?>

How should the code be changed to get output "equal"?

Answer

Tim Cooper picture Tim Cooper · Mar 6, 2011

You should modify your call for array_unique to have it include the SORT_REGULAR flag.

$arr2 = array_unique($arr, SORT_REGULAR);