PHP Getting Unique Values of a Multidimensional Array

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

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

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

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.

PHP: Finding distinct values from multidimensional associative array

PHP offers a wide range of convenience functions to handle array. I strongly suggest that you dive into the official documentation and start getting creative yourself:
http://php.net/manual/en/ref.array.php

Here is a simple example of the actual algorithm you asked using two such functions:

<?php  
$input = [
[
[
0 => "Jo Bloggs",
1 => "Cat",
2 => "Red",
3 => "Nissan"
],
[
0 => "Patrick",
1 => "Dog",
2 => "Blue",
3 => "Nissan"
],
],
[
[
0 => "Charloe",
1 => "Moose",
2 => "Green",
3 => "Ford"
],
[
0 => "Patrick",
1 => "Dog",
2 => "Blue",
3 => "Porsche"
]
]
];

$aspect = 1;
$output = [];
array_walk_recursive($input, function($value, $key) use ($aspect, &$output) {
if ($key == $aspect) {
$output[] = $value;
}
});

print_r(array_unique($output));

The output of above code obviously is:

Array
(
[0] => Cat
[1] => Dog
[2] => Moose
)

What type of elements are filtered out can be controlled via the $aspect variable, it holds the keys you want to identify matches with.

Obviously there are other approaches to what you asked, this is just a simple example. One might also prefer to accept only unique values in the $output array, for example, that would save the final call to array_unique()...

unique multidimensional array based on specific multiple keys

You can use a simple foreach loop to iterate through your array, then use the sub array key store_id to get the key of the final array. push in that final array the value of the current value.

Plus, to remove the duplicated item_type_id, I would write a function that checks if that id already exists in the result.

In example :


$storeID = [1000,1001];

$array = [
0 => [
"partner_id" => "10017",
"store_id" => "1000",
"item_type_id" => "2",
"value" => "58",
"category" => "1"
],
1 => [
"partner_id" => "10017",
"store_id" => "1000",
"item_type_id" => "1",
"value" => "63",
"category" => "1",
],
2 => [
"partner_id" => "0",
"store_id" => "1000",
"item_type_id" => "3",
"value" => "29",
"category" => "1"
],
3 => [
"partner_id" => "0",
"store_id" => "1000",
"item_type_id" => "2",
"value" => "58",
"category" => "1",
],
4 => [
"partner_id" => "0",
"store_id" => "1001",
"item_type_id" => "1",
"value" => "65",
"category" => "1",
],
5 => [
"partner_id" => "0",
"store_id" => "1001",
"item_type_id" => "2",
"value" => "58",
"category" => "1",
],
6 => [
"partner_id" => "0",
"store_id" => "1001",
"item_type_id" => "3",
"value" => "29",
"category" => "1"
],
7 => [
"partner_id" => "0",
"store_id" => "1000",
"item_type_id" => "1",
"value" => "65",
"category" => "1"
]
];

function ItemIdExists($arr, $itemId)
{
foreach ($arr as $subValue)
{
if ($subValue["item_type_id"] == $itemId)
{
return true;
}
}
return false;
}

$result = array();
foreach ($array as $value)
{
$key = $value["store_id"];
if (in_array($key, $storeID))
{
$ItemIdFound = isset($result[$key]) && ItemIdExists($result[$key], $value["item_type_id"]);

if (!$ItemIdFound)
$result[$key][] = $value;
}
}
var_dump($result);

Output

array(2) {
[1000]=>
array(3) {
[0]=>
array(5) {
["partner_id"]=>
string(5) "10017"
["store_id"]=>
string(4) "1000"
["item_type_id"]=>
string(1) "2"
["value"]=>
string(2) "58"
["category"]=>
string(1) "1"
}
[1]=>
array(5) {
["partner_id"]=>
string(5) "10017"
["store_id"]=>
string(4) "1000"
["item_type_id"]=>
string(1) "1"
["value"]=>
string(2) "63"
["category"]=>
string(1) "1"
}
[2]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1000"
["item_type_id"]=>
string(1) "3"
["value"]=>
string(2) "29"
["category"]=>
string(1) "1"
}
}
[1001]=>
array(3) {
[0]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1001"
["item_type_id"]=>
string(1) "1"
["value"]=>
string(2) "65"
["category"]=>
string(1) "1"
}
[1]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1001"
["item_type_id"]=>
string(1) "2"
["value"]=>
string(2) "58"
["category"]=>
string(1) "1"
}
[2]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1001"
["item_type_id"]=>
string(1) "3"
["value"]=>
string(2) "29"
["category"]=>
string(1) "1"
}
}
}


Related Topics



Leave a reply



Submit