How to Make a Multidimensional Array Unique

How to remove duplicate values from a multi-dimensional array in PHP

Here is another way. No intermediate variables are saved.

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

how to get array unique from multidimensional array when contains associative array?

You can use array_column to convert the array into an associative array. Which will override duplicate values. Use array_values to convert the associative array into a simple array.

$arr = //Your array

$result = array_values(array_column( $arr , null, 'product_code' ));

print_r( $result );

This will result to:

Array
(
[0] => Array
(
[product_code] => prod3
[qty] => 1
)

[1] => Array
(
[product_code] => prod4
[qty] => 7
)

)

Doc: array_column(), array_values()

How can you make a multidimensional array unique?

Since everyone given alternatives, here's a solution to the problem at-hand. Sometimes we have to work with the data we have, not re-arrange it the way we like it. That being said, this will remove all sub-sequent entries from the array that are duplicates.

$array = Array(
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Foo',
'slug' => 'Bar'
),
Array(
'name' => 'Test 1',
'slug' => 'test-1'
),
Array(
'name' => 'Test 2',
'slug' => 'test-2'
),
Array(
'name' => 'Test 3',
'slug' => 'test-3'
),
);
var_dump($array);

for ($e = 0; $e < count($array); $e++)
{
$duplicate = null;
for ($ee = $e+1; $ee < count($array); $ee++)
{
if (strcmp($array[$ee]['name'],$array[$e]['name']) === 0)
{
$duplicate = $ee;
break;
}
}
if (!is_null($duplicate))
array_splice($array,$duplicate,1);
}
var_dump($array);

Which will look like this:

array(6) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[4]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
[5]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
}
array(4) {
[0]=>
array(2) {
["name"]=>
string(6) "Test 3"
["slug"]=>
string(6) "test-3"
}
[1]=>
array(2) {
["name"]=>
string(3) "Foo"
["slug"]=>
string(3) "Bar"
}
[2]=>
array(2) {
["name"]=>
string(6) "Test 1"
["slug"]=>
string(6) "test-1"
}
[3]=>
array(2) {
["name"]=>
string(6) "Test 2"
["slug"]=>
string(6) "test-2"
}
}

array unique for multi dimensional array in php

Why are you making it so hard,

 $in = array (
1 =>
array (
'country' => 'US',
'state' => 'Albama',
'city' => 'Brest',
'postcode' => '225001-225003',
'shipping_info' => 'DeliveryAvailable',
'is_zip_range' => 1,
'zip_from' => 225001,
'zip_to' => 225003
),
2 =>
array (
'country' => 'BY',
'state' => 'Brest',
'city' => 'Brest',
'postcode' => '225001-225003',
'shipping_info' => 'DeliveryAvailable',
'is_zip_range' => 1,
'zip_from' => 225001,
'zip_to' => 225003
)
);

$out = [];
foreach($in as $i) if(!isset($out[$i['postcode']])) $out[$i['postcode']] = $i;

Sandbox

You can do the same thing with in_array, but the isset is faster.

In fact you dont even need to do the isset

foreach($in as $i) $out[$i['postcode']] = $i;

Array keys are always unique, but this will retain the last duplicate where as the previous code keeps the first one.

And if the keys bug you latter just do $out = array_values($out) to reset them.

How to get unique value in multidimensional array

Seems pretty simple: extract all pid values into their own array, run it through array_unique:

$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));

The same thing in longhand:

$pids = array();
foreach ($holder as $h) {
$pids[] = $h['pid'];
}
$uniquePids = array_unique($pids);

How to use FILTER on a multidimensional array to retrieve unique values?

Use reduce for getting the result.

var arr = [["apple","ghana",15],["apple","brazil",16],["orange","nigeria",10],["banana","bangladesh",20],["banana","ghana",6]];
const res = arr.reduce((a, c) => { if (!a.find(v => v[0] === c[0])) { a.push(c); } return a;}, []);
console.log(res);
.as-console-wrapper {  min-height: 100% !important;  top: 0;}

php getting unique values of a multidimensional array

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)));

How to get unique value in multidimensional array PHP

if $mainArray = array(2)... , try this code:

$mainString = "";
foreach($mainArray as $array){
foreach($array as $row){
$mainString .= $row;
} $mainString .= ", ";
}

$tempArray = explode(", ", $mainString);

$finishArray = array_unique($tempArray);

php remove duplicates from multidimensional array by value

Thanks everyone, sometimes over-thinking can affect common sense.

I simply solved it from MYSQL query with something like:

 GROUP BY list_title ORDER BY list_date


Related Topics



Leave a reply



Submit