Using array_unique() with multidimensional arrays

Permanent Link: Using array_unique() with multidimensional arrays 31. Januar 2009 RSS Feed for comments on RSS-Feed für Kommentare zu: Using array_unique() with multidimensional arrays comments feed

There's one problem with array_unique(): It doesn't work with multidimensional arrays. Here's an example:

$array = array(
array(
'id' => 123,
'name' => 'Some Product',
'ean' => '1234567890123'
),
array(
'id' => 123,
'name' => 'Some Product',
'ean' => '4852950174938'
),
array(
'id' => 123,
'name' => 'Some Product',
'ean' => '1234567890123'
),
);
$uniqueArray = array_unique($array);
var_dump($uniqueArray);

Two elements are exactly the same, but one element has a different EAN, yet the var_dump() returns the following:

array(1) {
[0]=>
array(3) {
["id"]=>
int(123)
["name"]=>
string(12) "Some Product"
["ean"]=>
string(13) "1234567890123"
}
}

Obviously this is unexpected behaviour. array_unique() threw out the second element, which is clearly not the same as Element 1 and 3. The easiest way I came across is using md5 hashes for comparison of the elements. All you need is to iterate over the first dimension, serialize it and create a MD5 hash of it for comparison:

/**
* Create Unique Arrays using an md5 hash
*
* @param array $array
* @return array
*/
function arrayUnique($array, $preserveKeys = false)
{
// Unique Array for return
$arrayRewrite = array();
// Array with the md5 hashes
$arrayHashes = array();
foreach($array as $key => $item) {
// Serialize the current element and create a md5 hash
$hash = md5(serialize($item));
// If the md5 didn't come up yet, add the element to
// to arrayRewrite, otherwise drop it
if (!isset($arrayHashes[$hash])) {
// Save the current element hash
$arrayHashes[$hash] = $hash;
// Add element to the unique Array
if ($preserveKeys) {
$arrayRewrite[$key] = $item;
} else {
$arrayRewrite[] = $item;
}
}
}
return $arrayRewrite;
}

$uniqueArray = arrayUnique($array);
var_dump($uniqueArray);

Now the result is the one array_unique() should have already given:

array(2) {
[0]=>
array(3) {
["id"]=>
int(123)
["name"]=>
string(12) "Some Product"
["ean"]=>
string(13) "1234567890123"
}
[1]=>
array(3) {
["id"]=>
int(123)
["name"]=>
string(12) "Some Product"
["ean"]=>
string(13) "4852950174938"
}
}

This works with as many dimensions as you like.

10 comments

AARONs Gravatar

AARON
17.12.2012, 06:37 o'clock

Very intresting…

shaons Gravatar

shaon
12.04.2013, 08:25 o'clock

Really its interesting…..cODE


17.07.2013, 15:43 o'clock

amols Gravatar

amol
31.07.2013, 08:32 o'clock

it is working good

s
02.11.2013, 05:10 o'clock

It works!
Thanks


24.03.2014, 11:16 o'clock

Atanas Dimitrovs Gravatar

Atanas Dimitrov
15.04.2014, 13:24 o'clock

Thanks!

I've been struggling with duped values for a while in my project, but this helped!

I had the same case as you:

$array = array(
array(
'x' => 123,
'y' => 37.12
),
array(
'x' => 123,
'y' => 37.12
),
array(
'x' => 123,
'y' => 37.12
),
array(
'x' => 124,
'y' => 38
),
);


The duplicate values are usually 3+ :D

Thanks again!

Rizwans Gravatar

Rizwan
24.05.2014, 09:22 o'clock

What I was looking for. Great work. Thanks buddy.

Aruns Gravatar

Arun
19.06.2014, 07:24 o'clock

Great Code.Working nicely.
Thanks !!

sarojs Gravatar

saroj
27.08.2014, 12:05 o'clock

Thanks its work fine…

Write a comment

(will not be published)