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.

9 comments


16.08.2017, 14:10 o'clock


18.09.2017, 09:39 o'clock


30.10.2017, 13:06 o'clock


01.11.2017, 14:00 o'clock


29.11.2017, 04:30 o'clock


13.12.2017, 07:16 o'clock

LastKelles Gravatar

LastKelle
12.01.2018, 11:47 o'clock

I see you don't monetize your blog, don't waste your traffic, you can earn extra bucks every month because you've got high quality content.
If you want to know how to make extra bucks, search for: Mertiso's tips best adsense alternative

SallieSmalls Gravatar

SallieSmall
14.01.2018, 12:38 o'clock

I have checked your page and i have found some duplicate content, that's why you don't rank high
in google, but there is a tool that can help you to create 100% unique articles, search for: Boorfe's tips unlimited content

BlairChiefs Gravatar

BlairChief
07.03.2018, 21:56 o'clock

I have checked your site and i've found some duplicate content, that's why
you don't rank high in google, but there is a tool that can help you to create 100% unique articles, search
for; Best article rewritwer Ercannou's essential tools

Write a comment

(will not be published)