Take the 2-minute tour ×
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
2  
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

3 Answers 3

up vote 12 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
function array_unique_when_values_are_serializable($main_array) {
    return array_map('unserialize', array_values(array_unique(array_map('serialize', $main_array))));
}
share|improve this answer
    
Please explain your answer. SO exists to teach users, not just answer questions. –  Machavity Apr 3 at 20:08

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.