32

Possible Duplicate:
php multi-dimensional array remove duplicate

I have an array like this:

$a = array ( 
    0 => array ( 'value' => 'America', ), 
    1 => array ( 'value' => 'England', ),  
    2 => array ( 'value' => 'Australia', ), 
    3 => array ( 'value' => 'America', ), 
    4 => array ( 'value' => 'England', ), 
    5 => array ( 'value' => 'Canada', ), 
)

How can I remove the duplicate values so that I get this:

$a = array ( 
    0 => array ( 'value' => 'America', ), 
    1 => array ( 'value' => 'England', ),  
    2 => array ( 'value' => 'Australia', ), 
    4 => array ( 'value' => 'Canada', ), 
)

I tried using array_unique, but that doesn't work due to this array being multidimensional, I think.

Edit: I also need this array to be multi-dimensional and in this format, I can't flatten it.

3
  • 13
    Ironically, several duplicates: stackoverflow.com/search?q=php+array+duplicate Commented Mar 14, 2010 at 13:17
  • 3
    they're all quite different? Show me on which answers the question of multidimensional arrays. Commented Mar 14, 2010 at 13:25
  • There's many, some for multi dimensional arrays, some for single dimensional arrays. The one I specifically voted to close on was: stackoverflow.com/questions/1861682/…, which is almost exactly the same question (removing elements based on a sub-element value). Please don't take it personally. It works better for SO if there aren't hundreds of duplicate questions. Commented Mar 14, 2010 at 13:52

3 Answers 3

86

array_unique is using string conversion before comparing the values to find the unique values:

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.

But an array will always convert to Array:

var_dump("Array" === (string) array());

You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique:

$unique = array_unique($a, SORT_REGULAR);

Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique to find the unique values:

$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
Sign up to request clarification or add additional context in comments.

5 Comments

That's why you need SORT_REGULAR flag, to keep arrays from conversion to strings. In theory, at least.
@Sejanus: That’s what I thought too. But it seems that it doesn’t work for Mark. So I proposed an alternative solution.
I like it when theres easy solutuons like this, thought I was going to have to do a lot of loops to solve this. Thanks!
This is great, looking for this a while
Hey try out this one-liner baby: $input = array_map('unserialize', array_unique(array_map('serialize', $input))); stackoverflow.com/a/946300/3063226
10

Here :)

<?php
 $a = array ( 
    0 => array ( 'value' => 'America', ), 
    1 => array ( 'value' => 'England', ),  
    2 => array ( 'value' => 'Australia', ), 
    3 => array ( 'value' => 'America', ), 
    4 => array ( 'value' => 'England', ), 
    5 => array ( 'value' => 'Canada', ), 
);

$tmp = array ();

foreach ($a as $row) 
    if (!in_array($row,$tmp)) array_push($tmp,$row);

print_r ($tmp);
?>

3 Comments

According to php.net, it's better to use $tmp[] = $row instead of array_push($tmp, $row). Otherwise: nice answer.
ah lol, you'r right, but now I'm a doubt about, why should I use array_push? :D ahah
array_push() is useful if you want to push more than one element into your array at one time OR if you want to use the return value (array count) that the function offers. Otherwise, I recommend [] as well. in_array() in a loop is a bad idea in general.
1

Use SORT_REGULAR flag.

$unique_array = array_unique($a, SORT_REGULAR);

I'm not sure why it helps but it does. At least with php 5.3

3 Comments

From the php docs: Note: Note that array_unique() is not intended to work on multi dimensional arrays.
Oh, I'm on 5.2, it's not working for me.
Well probably you will need to write your own function in that case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.