Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php getting unique values of a multidimensional array [duplicate]

Tags:

arrays

php

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.

like image 246
Mark Avatar asked Mar 14 '10 13:03

Mark


People also ask

How can we get duplicate values in multidimensional array in PHP?

To merge the duplicate value in a multidimensional array in PHP, first, create an empty array that will contain the final result. Then we iterate through each element in the array and check for its duplicity by comparing it with other elements.

How do you get unique values in a multidimensional array?

A user-defined function can help in getting unique values from multidimensional arrays without considering the keys. You can use the PHP array unique function along with the other array functions to get unique values from a multidimensional array while preserving the keys.

How can I get unique values from two arrays in PHP?

You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.

How do you find duplicates in a 2d array?

Here's a brute-force straight forward way of counting duplicates. Turn the 2d array into a 1d array ( List<Integer> ), then loop through the 1d array counting the duplicates as you find them and removing them so you don't count them more than once.


3 Answers

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)));
like image 154
Gumbo Avatar answered Nov 02 '22 04:11

Gumbo


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);
?>
like image 10
Marcx Avatar answered Nov 02 '22 05:11

Marcx


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

like image 1
Sejanus Avatar answered Nov 02 '22 05:11

Sejanus