Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need 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 be code fix to get output "equal"?

Thanks, Yosef

share|improve this question
Man, this always comes up. Read the manual, it says "Note that array_unique() is not intended to work on multi dimensional arrays." – BoltClock Mar 6 '11 at 17:00
I asking for "like" solution, please read my question – Yosef Mar 6 '11 at 17:16
I don't understand what you mean by "Hi, like array_unique for arrays inside array." – BoltClock Mar 6 '11 at 17:31

2 Answers

up vote 3 down vote accepted

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

$arr2 = array_unique($arr, SORT_REGULAR);
share|improve this answer

If you want to test if the outer array has unique entries, then stringify the inner contents first for a comparison:

$arr1 = array_map("serialize", $arr);
$arr2 = array_unique($arr1);
if ($arr2 == $arr1) {
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.