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.

Community
  • 1
  • 1
Mark
  • 32,293
  • 33
  • 107
  • 137
  • 13
    Ironically, several duplicates: http://stackoverflow.com/search?q=php+array+duplicate – Andy E Mar 14 '10 at 13:17
  • 3
    they're all quite different? Show me on which answers the question of multidimensional arrays. – Mark Mar 14 '10 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: http://stackoverflow.com/questions/1861682/php-multi-dimensional-array-remove-duplicate, 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. – Andy E Mar 14 '10 at 13:52

3 Answers3

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)));
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • That's why you need SORT_REGULAR flag, to keep arrays from conversion to strings. In theory, at least. – Sejanus Mar 14 '10 at 13:51
  • @Sejanus: That’s what I thought too. But it seems that it doesn’t work for Mark. So I proposed an alternative solution. – Gumbo Mar 14 '10 at 14:09
  • 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! – Daniel West Oct 13 '11 at 10:44
  • This is great, looking for this a while – Ole_S Aug 13 '14 at 07:31
  • Hey try out this one-liner baby: `$input = array_map('unserialize', array_unique(array_map('serialize', $input)));` https://stackoverflow.com/a/946300/3063226 – Heitor Aug 23 '17 at 08:21
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);
?>
Marcx
  • 6,806
  • 5
  • 46
  • 69
  • 1
    According to [php.net](http://php.net/array_push), it's better to use `$tmp[] = $row` instead of `array_push($tmp, $row)`. Otherwise: nice answer. – middus Mar 14 '10 at 15:23
  • ah lol, you'r right, but now I'm a doubt about, why should I use array_push? :D ahah – Marcx Mar 14 '10 at 18:04
  • `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. – mickmackusa Jul 21 '21 at 13:44
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

Sejanus
  • 3,253
  • 8
  • 39
  • 52