Shuffling the first level of the array in PHP - php

PHP's shuffle() is not randomizing an array the way I need it. I have a two dimensional array and when I use shuffle() on it it only randomizes the 2nd dimension of the array, but I need the opposite.
Lets assume this is the array I need to shuffle:
Array
(
[0] => Array
(
[key1] => 199
[key2] => 6
)
[1] => Array
(
[key1] => 195
[key2] => 3
)
)
The way the shuffle() shuffles it is like this:
Array
(
[0] => Array
(
[key1] => 195
[key2] => 3
)
[1] => Array
(
[key1] => 199
[key2] => 6
)
)
But this is not what I'm after. What I need as an end result is this:
Array
(
[1] => Array
(
[key1] => 195
[key2] => 6
)
[0] => Array
(
[key1] => 199
[key2] => 6
)
)
I know that this can be achieved this using a random key with rand() or mt_rand(), but it also could be possible that for a small amount of keys, we could receive the same rand() key twice, leading to NOT have a nicely shuffled array.
I also know that adding more if else logic would be a possibility, but I'm looking to do this with already implemented stuff - I don't wanna reinvent the wheel.
How can I achieve my desired shuffle?

shuffle() is working as intended. It is not "randomizing the 2nd dimension", it is not recursive.
It is reordering the elements of the array (which just happen to be arrays). The issue you are seeing is because shuffle() resets the array's keys.
From the docs (http://php.net/shuffle):
Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than
just reordering the keys.
To get what you want, you need to use array_rand() to randomize the keys, then reorder the elements in the array based on that.
$randKeys = array_rand($array, count($array));
// This is needed because array_rand was changed
// and now returns the keys in order
shuffle($randKeys);
uksort($array, function($a, $b) use($randKeys){
return array_search($a, $randKeys) - array_search($b, $randKeys);
});
DEMO: https://eval.in/101265

In the comment section of the PHP manual for shuffle you find:
<?php
function shuffle_assoc(&$array) {
$keys = array_keys($array);
shuffle($keys);
foreach($keys as $key) {
$new[$key] = $array[$key];
}
$array = $new;
return true;
}
?>

$yourArray = array(
array('key1' => 199, 'key2' => 6),
array('key1' => 195, 'key2' => 3),
array('key1' => 205, 'key2' => 8)
);
$helperArr = array();
foreach($yourArray as $subArr)
{
foreach($subArr as $key => $value)
$helperArr[$key][] = $value;
}
foreach($helperArr as &$shuffleArr)
shuffle($shuffleArr);
$shuffledArr = array();
foreach($helperArr as $key => $value)
{
for($i = 0; $i < count($value); $i++)
$shuffledArr[$i][$key] = $value[$i];
}
echo '<pre>';
print_r($helperArr);
print_r($shuffledArr);
echo '</pre>';
DEMO

Related

Remove duplicates from a php array by a specific multi-dimensional array key that is nested [duplicate]

How can I remove duplicate values from a multi-dimensional array in PHP?
Example array:
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
[3] => Array
(
[0] => abc
[1] => def
)
[4] => Array
(
[0] => ghi
[1] => jkl
)
[5] => Array
(
[0] => mno
[1] => pql
)
)
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)));
Since 5.2.9 you can use array_unique() if you use the SORT_REGULAR flag like so:
array_unique($array, SORT_REGULAR);
This makes the function compare elements for equality as if $a == $b were being used, which is perfect for your case.
Output
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
)
Keep in mind, though, that the documentation states:
array_unique() is not intended to work on multi dimensional arrays.
I had a similar problem but I found a 100% working solution for it.
<?php
function super_unique($array,$key)
{
$temp_array = [];
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr="";
$arr[0]['id']=0;
$arr[0]['titel']="ABC";
$arr[1]['id']=1;
$arr[1]['titel']="DEF";
$arr[2]['id']=2;
$arr[2]['titel']="ABC";
$arr[3]['id']=3;
$arr[3]['titel']="XYZ";
echo "<pre>";
print_r($arr);
echo "unique*********************<br/>";
print_r(super_unique($arr,'titel'));
?>
Another way. Will preserve keys as well.
function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
Array
(
[0] => Array
(
[id] => 1
[name] => john
)
[1] => Array
(
[id] => 2
[name] => smith
)
[2] => Array
(
[id] => 3
[name] => john
)
[3] => Array
(
[id] => 4
[name] => robert
)
)
$temp = array_unique(array_column($array, 'name'));
$unique_arr = array_intersect_key($array, $temp);
This will remove the duplicate names from array. unique by key
If "remove duplicates" means "remove duplicates, but let one there", a solution might be to apply the array_unique(...) on the "identifier column" first and then to remove in the original array all the keys, that have been removed from the column array:
$array = [
[
'id' => '123',
'foo' => 'aaa',
'bar' => 'bbb'
],
[
'id' => '123',
'foo' => 'ccc',
'bar' => 'ddd'
],
[
'id' => '567',
'foo' => 'eee',
'bar' => 'fff'
]
];
$ids = array_column($array, 'id');
$ids = array_unique($ids);
$array = array_filter($array, function ($key, $value) use ($ids) {
return in_array($value, array_keys($ids));
}, ARRAY_FILTER_USE_BOTH);
The result is:
Array
(
[0] => Array
(
[id] => 123
[foo] => aaa
[bar] => bbb
)
[2] => Array
(
[id] => 567
[foo] => eee
[bar] => fff
)
)
The user comments on the array_unique() documentation have many solutions to this. Here is one of them:
kenrbnsn at rbnsn dot com
27-Sep-2005 12:09
Yet another Array_Unique for multi-demensioned arrays. I've only tested this on two-demensioned arrays, but it could probably be generalized for more, or made to use recursion.
This function uses the serialize, array_unique, and unserialize functions to do the work.
function multi_unique($array) {
foreach ($array as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
This is from http://ca3.php.net/manual/en/function.array-unique.php#57202.
if you need to eliminate duplicates on specific keys, such as a mysqli id, here's a simple funciton
function search_array_compact($data,$key){
$compact = [];
foreach($data as $row){
if(!in_array($row[$key],$compact)){
$compact[] = $row;
}
}
return $compact;
}
Bonus Points
You can pass an array of keys and add an outer foreach, but it will be 2x slower per additional key.
if you have an array like this:
(users is the name of the array)
Array=>
[0] => (array)
'user' => 'john'
'age' => '23'
[1] => (array)
'user' => 'jane'
'age' => '20'
[2]=> (array)
'user' => 'john'
'age' => '23'
and you want to delete duplicates...then:
$serialized = array();
for ($i=0; $i < sizeof($users); $i++) {
$test = in_array($users['user'], $serialized);
if ($test == false) {
$serialized[] = $users['user'];
}
}
can be a solution :P
Lots of person asked me how to make Unique multidimensional array. I have taken reference from your comment and it helps me.
First of All, Thanks to #jeromegamez #daveilers for your solution. But every time i gave the answer, they asked me how this 'serialize' and 'unserialize' works. That's why i want to share the reason of this with you so that it will help more people to understand the concept behind this.
I am explaining why we use 'serialize' and 'unserialize' in steps :
Step 1: Convert the multidimensional array to one-dimensional array
To convert the multidimensional array to a one-dimensional array, first generate byte stream representation of all the elements (including nested arrays) inside the array. serialize() function can generate byte stream representation of a value. To generate byte stream representation of all the elements, call serialize() function inside array_map() function as a callback function. The result will be a one dimensional array no matter how many levels the multidimensional array has.
Step 2: Make the values unique
To make this one dimensional array unique, use array_unique() function.
Step 3: Revert it to the multidimensional array
Though the array is now unique, the values looks like byte stream representation. To revert it back to the multidimensional array, use unserialize() function.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Thanks again for all this.
A very easy and logical way to Unique a multi dimension array is as follows,
If you have array like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value1
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value4
)
)
use foreach to solve this:
foreach($array as $k=>$v){
$unique=array_unique($v);
$array[$k]=$unique;
}
it will give you following result:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
[4] => Value4
)
)
and if you want to rearrange the order of the keys,
foreach($array as $k=>$v){
$unique= array_values(array_unique($v));
$array[$k]=$unique;
}
This operation will give you arranged key values like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
[3] => Value4
)
)
I hope this will clear everything.
An easy to read solution, probably not the most efficient:
function arrayUnique($myArray){
if(!is_array($myArray))
return $myArray;
foreach ($myArray as &$myvalue){
$myvalue=serialize($myvalue);
}
$myArray=array_unique($myArray);
foreach ($myArray as &$myvalue){
$myvalue=unserialize($myvalue);
}
return $myArray;
}
As people are saying array_unique() is very slow, here is a snippet I use for one level multidimensional array.
$serialized_array = array_map("serialize", $input);
foreach ($serialized_array as $key => $val) {
$result[$val] = true;
}
$output = array_map("unserialize", (array_keys($result)));
Reference first user contributed note of array_unique() function page in php.net
This solution is relevant only when uniqueness is needed for one array column, for example here if we need the uniqueness in index #0 of the arrays.
Solution #1:
Using array_filter with an anonymous function and a static variable:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$list = array_filter($list, function ($item) {
static $values = [];
if (!in_array($item[0], $values)) {
$values[] = $item[0];
return true;
} else {
return false;
}
});
var_dump($list);
Solution #2:
Since the value when we want the uniqueness are of string type, we can remap the original global array to use these values as keys, which will remove duplicates as we remap it:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$unique = [];
foreach ($list as $item) {
$unique[$item[0]] = $item;
}
// Remap again to integers
$unique = array_values($unique);
var_dump($unique);
An alternative to serialize and unique
$test = [
['abc','def'],
['ghi','jkl'],
['mno','pql'],
['abc','def'],
['ghi','jkl'],
['mno','pql'],
];
$result = array_reduce(
$test,
function($carry,$item){
if(!in_array($item,$carry)) {
array_push($carry,$item);
}
return $carry;
},
[]
);
var_dump($result);
/*
php unique.php
array(3) {
[0] =>
array(2) {
[0] =>
string(3) "abc"
[1] =>
string(3) "def"
}
[1] =>
array(2) {
[0] =>
string(3) "ghi"
[1] =>
string(3) "jkl"
}
[2] =>
array(2) {
[0] =>
string(3) "mno"
[1] =>
string(3) "pql"
}
}
*/
I've given this problem a lot of thought and have determined that the optimal solution should follow two rules.
For scalability, modify the array in place; no copying to a new array
For performance, each comparison should be made only once
With that in mind and given all of PHP's quirks, below is the solution I came up with. Unlike some of the other answers, it has the ability to remove elements based on whatever key(s) you want. The input array is expected to be numeric keys.
$count_array = count($input);
for ($i = 0; $i < $count_array; $i++) {
if (isset($input[$i])) {
for ($j = $i+1; $j < $count_array; $j++) {
if (isset($input[$j])) {
//this is where you do your comparison for dupes
if ($input[$i]['checksum'] == $input[$j]['checksum']) {
unset($input[$j]);
}
}
}
}
}
The only drawback is that the keys are not in order when the iteration completes. This isn't a problem if you're subsequently using only foreach loops, but if you need to use a for loop, you can put $input = array_values($input); after the above to renumber the keys.
try this solution for (n) Dimensional array with non-restricted length
for example this array
$arr= [
0 => [0=>"a" , 1=>"b" , 2=>"c" ] ,
1 => [0=>"x" , 1=>"b" , 2=>"a", 3=>"p"],
2=> [
[
0=>"y" ,
1=>"b" ,
2=> [0=>"x" , 1=>"m" , 2=>"a"]
],
1=>"z" ,
2=>"v"
]
];
This would be the solution
$ar2=[];
$ar3=[];
function test($arr){
global $ar2,$ar3;
if(is_array($arr)){
return array_map("test",$arr);
}
if(!isset($ar2[$arr])){
$ar2[$arr]=1;
$ar3[]=$arr;
}
}
array_map("test",$arr);
print_r($ar3);
Based on the Answer marked as correct, adding my answer. Small code added just to reset the indices-
$input = array_values(array_map("unserialize", array_unique(array_map("serialize", $inputArray))));

Array merge with same keys

I have these three arrays:
Array
(
[1] => sadsad#fsdf.fgh
[2] => rtt#RERT.FDG
[3] => WQEWQ#fgdg.h
)
Array
(
[1] =>
[2] => 4234235
[3] =>
)
Array
(
[2] => 1
)
And I want to generate this output:
Array
(
[1] => array(
[0] => sadsad#fsdf.fgh
)
[2] => array(
[0] => rtt#RERT.FDG
[1] => 4234235
[2] => 1
)
[3] => array(
[0] => WQEWQ#fgdg.h
)
)
I need some assistance because I already researched array_merge_recursive() and array_merge(), but I can't get the correct result.
If I need to use foreach() what must I do to merge these 3 arrays.
Wrote a little script:
$a = array
(
1=>"sadsad#fsdf.fgh",
2=>"rtt#RERT.FDG",
3=>"WQEWQ#fgdg.h",
);
$b = array
(
2 => 4234235
);
$c = array
(
2 => 1
);
$arrayKeys = array_unique(
array_merge(
array_keys($a),
array_keys($b),
array_keys($c)
)
);
$d = array_combine(
$arrayKeys,
array_fill(
0,
count($arrayKeys),
array()
)
);
foreach($a as $key => $value) {
if(!empty($a[$key])) {
$d[$key][] = $a[$key];
}
if(!empty($b[$key])) {
$d[$key][] = $b[$key];
}
if(!empty($c[$key])) {
$d[$key][] = $c[$key];
}
}
var_dump($d);
Also if you want to you can merge together the arrays using the variable names only
//names of the variables to merge together
$arrayVariableNames = array("a","b","c");
//merging array keys together
$arrayKeys = array();
foreach($arrayVariableNames as $variableName) {
$arrayKeys = array_merge(
$arrayKeys,
array_keys(${$variableName})
);
}
$arrayKeys = array_unique($arrayKeys);
//initialize the result array with empty arrays
$resultArray = array_combine(
$arrayKeys,
array_fill(
0,
count($arrayKeys),
array()
)
);
//loop through all the keys and add the elements from all the arrays
foreach($resultArray as $key => &$value) {
foreach($arrayVariableNames as $variableName) {
if(!empty(${$variableName}[$key])) {
$value[] = ${$variableName}[$key];
}
}
}
As you have no doubt discovered, array_merge_recursive() stubbornly smashes all numeric or "numeric string" keys into a 1-dimensional array. To avoid this behavior, you need to cast each of your arrays' initial keys as strings in a way that will not be assumed to be a number by array_merge_recursive().
Additionally you want to filter out all elements that have empty values.
I initially wrote a one-liner that performed the key re-casting then filtered the values, but it is less efficient that way. For your case, you should only use array_filter() on arrays that may possibly contain empty values.
Input Arrays:
$a=[1=>"sadsad#fsdf.fgh",2=>"rtt#RERT.FDG",3=>"WQEWQ#fgdg.h"];
$b=[1=>"",2=>"4234235",3=>""];
$c=[2=>1];
Code:
// remove empty values from all arrays that may have them
$b=array_filter($b,'strlen');
// for all arrays, cast numeric keys to string by prepending with a space
function addK($v){return " $v";}
$a=array_combine(array_map('addK',array_keys($a)),$a);
$b=array_combine(array_map('addK',array_keys($b)),$b);
$c=array_combine(array_map('addK',array_keys($c)),$c);
// merge arrays recursively
$merged=array_merge_recursive($a,$b,$c);
// cast keys back to numeric
$merged=array_combine(array_map('trim',array_keys($merged)),$merged);
// force all top-level elements to be arrays
foreach($merged as $k=>$v){
if(is_string($merged[$k])){$merged[$k]=[$v];}
}
var_export($merged);
Output:
array (
1 => array (
0 => 'sadsad#fsdf.fgh',
),
2 => array (
0 => 'rtt#RERT.FDG',
1 => '4234235',
2 => 1,
),
3 => array (
0 => 'WQEWQ#fgdg.h',
),
)
For readers who want to know the difference when array_merge_recursive() is run with no preparation:
array (
0 => 'sadsad#fsdf.fgh',
1 => 'rtt#RERT.FDG',
2 => 'WQEWQ#fgdg.h',
3 => '',
4 => '4234235',
5 => '',
6 => 1,
)
Notice the 1d array and the re-indexed keys? ...totally useless for the OP.
Finally, for anyone who wants to re-cast the keys to all arrays and would like to make my process more DRY, there may be an opportunity to set up a variadic function or similar. I merely didn't bother to pursue the notion because I didn't want to make my answer anymore complex and it is not a terrible amount of Repeating Myself.

Remove duplicates based on inner value from multidimensional arrays [duplicate]

How can I remove duplicate values from a multi-dimensional array in PHP?
Example array:
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
[3] => Array
(
[0] => abc
[1] => def
)
[4] => Array
(
[0] => ghi
[1] => jkl
)
[5] => Array
(
[0] => mno
[1] => pql
)
)
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)));
Since 5.2.9 you can use array_unique() if you use the SORT_REGULAR flag like so:
array_unique($array, SORT_REGULAR);
This makes the function compare elements for equality as if $a == $b were being used, which is perfect for your case.
Output
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
)
Keep in mind, though, that the documentation states:
array_unique() is not intended to work on multi dimensional arrays.
I had a similar problem but I found a 100% working solution for it.
<?php
function super_unique($array,$key)
{
$temp_array = [];
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr="";
$arr[0]['id']=0;
$arr[0]['titel']="ABC";
$arr[1]['id']=1;
$arr[1]['titel']="DEF";
$arr[2]['id']=2;
$arr[2]['titel']="ABC";
$arr[3]['id']=3;
$arr[3]['titel']="XYZ";
echo "<pre>";
print_r($arr);
echo "unique*********************<br/>";
print_r(super_unique($arr,'titel'));
?>
Another way. Will preserve keys as well.
function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
Array
(
[0] => Array
(
[id] => 1
[name] => john
)
[1] => Array
(
[id] => 2
[name] => smith
)
[2] => Array
(
[id] => 3
[name] => john
)
[3] => Array
(
[id] => 4
[name] => robert
)
)
$temp = array_unique(array_column($array, 'name'));
$unique_arr = array_intersect_key($array, $temp);
This will remove the duplicate names from array. unique by key
If "remove duplicates" means "remove duplicates, but let one there", a solution might be to apply the array_unique(...) on the "identifier column" first and then to remove in the original array all the keys, that have been removed from the column array:
$array = [
[
'id' => '123',
'foo' => 'aaa',
'bar' => 'bbb'
],
[
'id' => '123',
'foo' => 'ccc',
'bar' => 'ddd'
],
[
'id' => '567',
'foo' => 'eee',
'bar' => 'fff'
]
];
$ids = array_column($array, 'id');
$ids = array_unique($ids);
$array = array_filter($array, function ($key, $value) use ($ids) {
return in_array($value, array_keys($ids));
}, ARRAY_FILTER_USE_BOTH);
The result is:
Array
(
[0] => Array
(
[id] => 123
[foo] => aaa
[bar] => bbb
)
[2] => Array
(
[id] => 567
[foo] => eee
[bar] => fff
)
)
The user comments on the array_unique() documentation have many solutions to this. Here is one of them:
kenrbnsn at rbnsn dot com
27-Sep-2005 12:09
Yet another Array_Unique for multi-demensioned arrays. I've only tested this on two-demensioned arrays, but it could probably be generalized for more, or made to use recursion.
This function uses the serialize, array_unique, and unserialize functions to do the work.
function multi_unique($array) {
foreach ($array as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
This is from http://ca3.php.net/manual/en/function.array-unique.php#57202.
if you need to eliminate duplicates on specific keys, such as a mysqli id, here's a simple funciton
function search_array_compact($data,$key){
$compact = [];
foreach($data as $row){
if(!in_array($row[$key],$compact)){
$compact[] = $row;
}
}
return $compact;
}
Bonus Points
You can pass an array of keys and add an outer foreach, but it will be 2x slower per additional key.
if you have an array like this:
(users is the name of the array)
Array=>
[0] => (array)
'user' => 'john'
'age' => '23'
[1] => (array)
'user' => 'jane'
'age' => '20'
[2]=> (array)
'user' => 'john'
'age' => '23'
and you want to delete duplicates...then:
$serialized = array();
for ($i=0; $i < sizeof($users); $i++) {
$test = in_array($users['user'], $serialized);
if ($test == false) {
$serialized[] = $users['user'];
}
}
can be a solution :P
Lots of person asked me how to make Unique multidimensional array. I have taken reference from your comment and it helps me.
First of All, Thanks to #jeromegamez #daveilers for your solution. But every time i gave the answer, they asked me how this 'serialize' and 'unserialize' works. That's why i want to share the reason of this with you so that it will help more people to understand the concept behind this.
I am explaining why we use 'serialize' and 'unserialize' in steps :
Step 1: Convert the multidimensional array to one-dimensional array
To convert the multidimensional array to a one-dimensional array, first generate byte stream representation of all the elements (including nested arrays) inside the array. serialize() function can generate byte stream representation of a value. To generate byte stream representation of all the elements, call serialize() function inside array_map() function as a callback function. The result will be a one dimensional array no matter how many levels the multidimensional array has.
Step 2: Make the values unique
To make this one dimensional array unique, use array_unique() function.
Step 3: Revert it to the multidimensional array
Though the array is now unique, the values looks like byte stream representation. To revert it back to the multidimensional array, use unserialize() function.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Thanks again for all this.
A very easy and logical way to Unique a multi dimension array is as follows,
If you have array like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value1
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value4
)
)
use foreach to solve this:
foreach($array as $k=>$v){
$unique=array_unique($v);
$array[$k]=$unique;
}
it will give you following result:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
[4] => Value4
)
)
and if you want to rearrange the order of the keys,
foreach($array as $k=>$v){
$unique= array_values(array_unique($v));
$array[$k]=$unique;
}
This operation will give you arranged key values like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
[3] => Value4
)
)
I hope this will clear everything.
An easy to read solution, probably not the most efficient:
function arrayUnique($myArray){
if(!is_array($myArray))
return $myArray;
foreach ($myArray as &$myvalue){
$myvalue=serialize($myvalue);
}
$myArray=array_unique($myArray);
foreach ($myArray as &$myvalue){
$myvalue=unserialize($myvalue);
}
return $myArray;
}
As people are saying array_unique() is very slow, here is a snippet I use for one level multidimensional array.
$serialized_array = array_map("serialize", $input);
foreach ($serialized_array as $key => $val) {
$result[$val] = true;
}
$output = array_map("unserialize", (array_keys($result)));
Reference first user contributed note of array_unique() function page in php.net
This solution is relevant only when uniqueness is needed for one array column, for example here if we need the uniqueness in index #0 of the arrays.
Solution #1:
Using array_filter with an anonymous function and a static variable:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$list = array_filter($list, function ($item) {
static $values = [];
if (!in_array($item[0], $values)) {
$values[] = $item[0];
return true;
} else {
return false;
}
});
var_dump($list);
Solution #2:
Since the value when we want the uniqueness are of string type, we can remap the original global array to use these values as keys, which will remove duplicates as we remap it:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$unique = [];
foreach ($list as $item) {
$unique[$item[0]] = $item;
}
// Remap again to integers
$unique = array_values($unique);
var_dump($unique);
An alternative to serialize and unique
$test = [
['abc','def'],
['ghi','jkl'],
['mno','pql'],
['abc','def'],
['ghi','jkl'],
['mno','pql'],
];
$result = array_reduce(
$test,
function($carry,$item){
if(!in_array($item,$carry)) {
array_push($carry,$item);
}
return $carry;
},
[]
);
var_dump($result);
/*
php unique.php
array(3) {
[0] =>
array(2) {
[0] =>
string(3) "abc"
[1] =>
string(3) "def"
}
[1] =>
array(2) {
[0] =>
string(3) "ghi"
[1] =>
string(3) "jkl"
}
[2] =>
array(2) {
[0] =>
string(3) "mno"
[1] =>
string(3) "pql"
}
}
*/
I've given this problem a lot of thought and have determined that the optimal solution should follow two rules.
For scalability, modify the array in place; no copying to a new array
For performance, each comparison should be made only once
With that in mind and given all of PHP's quirks, below is the solution I came up with. Unlike some of the other answers, it has the ability to remove elements based on whatever key(s) you want. The input array is expected to be numeric keys.
$count_array = count($input);
for ($i = 0; $i < $count_array; $i++) {
if (isset($input[$i])) {
for ($j = $i+1; $j < $count_array; $j++) {
if (isset($input[$j])) {
//this is where you do your comparison for dupes
if ($input[$i]['checksum'] == $input[$j]['checksum']) {
unset($input[$j]);
}
}
}
}
}
The only drawback is that the keys are not in order when the iteration completes. This isn't a problem if you're subsequently using only foreach loops, but if you need to use a for loop, you can put $input = array_values($input); after the above to renumber the keys.
try this solution for (n) Dimensional array with non-restricted length
for example this array
$arr= [
0 => [0=>"a" , 1=>"b" , 2=>"c" ] ,
1 => [0=>"x" , 1=>"b" , 2=>"a", 3=>"p"],
2=> [
[
0=>"y" ,
1=>"b" ,
2=> [0=>"x" , 1=>"m" , 2=>"a"]
],
1=>"z" ,
2=>"v"
]
];
This would be the solution
$ar2=[];
$ar3=[];
function test($arr){
global $ar2,$ar3;
if(is_array($arr)){
return array_map("test",$arr);
}
if(!isset($ar2[$arr])){
$ar2[$arr]=1;
$ar3[]=$arr;
}
}
array_map("test",$arr);
print_r($ar3);
Based on the Answer marked as correct, adding my answer. Small code added just to reset the indices-
$input = array_values(array_map("unserialize", array_unique(array_map("serialize", $inputArray))));

How do I merge array values fo a multidimensional array [duplicate]

How can I remove duplicate values from a multi-dimensional array in PHP?
Example array:
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
[3] => Array
(
[0] => abc
[1] => def
)
[4] => Array
(
[0] => ghi
[1] => jkl
)
[5] => Array
(
[0] => mno
[1] => pql
)
)
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)));
Since 5.2.9 you can use array_unique() if you use the SORT_REGULAR flag like so:
array_unique($array, SORT_REGULAR);
This makes the function compare elements for equality as if $a == $b were being used, which is perfect for your case.
Output
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
)
Keep in mind, though, that the documentation states:
array_unique() is not intended to work on multi dimensional arrays.
I had a similar problem but I found a 100% working solution for it.
<?php
function super_unique($array,$key)
{
$temp_array = [];
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr="";
$arr[0]['id']=0;
$arr[0]['titel']="ABC";
$arr[1]['id']=1;
$arr[1]['titel']="DEF";
$arr[2]['id']=2;
$arr[2]['titel']="ABC";
$arr[3]['id']=3;
$arr[3]['titel']="XYZ";
echo "<pre>";
print_r($arr);
echo "unique*********************<br/>";
print_r(super_unique($arr,'titel'));
?>
Another way. Will preserve keys as well.
function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
Array
(
[0] => Array
(
[id] => 1
[name] => john
)
[1] => Array
(
[id] => 2
[name] => smith
)
[2] => Array
(
[id] => 3
[name] => john
)
[3] => Array
(
[id] => 4
[name] => robert
)
)
$temp = array_unique(array_column($array, 'name'));
$unique_arr = array_intersect_key($array, $temp);
This will remove the duplicate names from array. unique by key
If "remove duplicates" means "remove duplicates, but let one there", a solution might be to apply the array_unique(...) on the "identifier column" first and then to remove in the original array all the keys, that have been removed from the column array:
$array = [
[
'id' => '123',
'foo' => 'aaa',
'bar' => 'bbb'
],
[
'id' => '123',
'foo' => 'ccc',
'bar' => 'ddd'
],
[
'id' => '567',
'foo' => 'eee',
'bar' => 'fff'
]
];
$ids = array_column($array, 'id');
$ids = array_unique($ids);
$array = array_filter($array, function ($key, $value) use ($ids) {
return in_array($value, array_keys($ids));
}, ARRAY_FILTER_USE_BOTH);
The result is:
Array
(
[0] => Array
(
[id] => 123
[foo] => aaa
[bar] => bbb
)
[2] => Array
(
[id] => 567
[foo] => eee
[bar] => fff
)
)
The user comments on the array_unique() documentation have many solutions to this. Here is one of them:
kenrbnsn at rbnsn dot com
27-Sep-2005 12:09
Yet another Array_Unique for multi-demensioned arrays. I've only tested this on two-demensioned arrays, but it could probably be generalized for more, or made to use recursion.
This function uses the serialize, array_unique, and unserialize functions to do the work.
function multi_unique($array) {
foreach ($array as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
This is from http://ca3.php.net/manual/en/function.array-unique.php#57202.
if you need to eliminate duplicates on specific keys, such as a mysqli id, here's a simple funciton
function search_array_compact($data,$key){
$compact = [];
foreach($data as $row){
if(!in_array($row[$key],$compact)){
$compact[] = $row;
}
}
return $compact;
}
Bonus Points
You can pass an array of keys and add an outer foreach, but it will be 2x slower per additional key.
if you have an array like this:
(users is the name of the array)
Array=>
[0] => (array)
'user' => 'john'
'age' => '23'
[1] => (array)
'user' => 'jane'
'age' => '20'
[2]=> (array)
'user' => 'john'
'age' => '23'
and you want to delete duplicates...then:
$serialized = array();
for ($i=0; $i < sizeof($users); $i++) {
$test = in_array($users['user'], $serialized);
if ($test == false) {
$serialized[] = $users['user'];
}
}
can be a solution :P
Lots of person asked me how to make Unique multidimensional array. I have taken reference from your comment and it helps me.
First of All, Thanks to #jeromegamez #daveilers for your solution. But every time i gave the answer, they asked me how this 'serialize' and 'unserialize' works. That's why i want to share the reason of this with you so that it will help more people to understand the concept behind this.
I am explaining why we use 'serialize' and 'unserialize' in steps :
Step 1: Convert the multidimensional array to one-dimensional array
To convert the multidimensional array to a one-dimensional array, first generate byte stream representation of all the elements (including nested arrays) inside the array. serialize() function can generate byte stream representation of a value. To generate byte stream representation of all the elements, call serialize() function inside array_map() function as a callback function. The result will be a one dimensional array no matter how many levels the multidimensional array has.
Step 2: Make the values unique
To make this one dimensional array unique, use array_unique() function.
Step 3: Revert it to the multidimensional array
Though the array is now unique, the values looks like byte stream representation. To revert it back to the multidimensional array, use unserialize() function.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Thanks again for all this.
A very easy and logical way to Unique a multi dimension array is as follows,
If you have array like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value1
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value4
)
)
use foreach to solve this:
foreach($array as $k=>$v){
$unique=array_unique($v);
$array[$k]=$unique;
}
it will give you following result:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
[4] => Value4
)
)
and if you want to rearrange the order of the keys,
foreach($array as $k=>$v){
$unique= array_values(array_unique($v));
$array[$k]=$unique;
}
This operation will give you arranged key values like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
[3] => Value4
)
)
I hope this will clear everything.
An easy to read solution, probably not the most efficient:
function arrayUnique($myArray){
if(!is_array($myArray))
return $myArray;
foreach ($myArray as &$myvalue){
$myvalue=serialize($myvalue);
}
$myArray=array_unique($myArray);
foreach ($myArray as &$myvalue){
$myvalue=unserialize($myvalue);
}
return $myArray;
}
As people are saying array_unique() is very slow, here is a snippet I use for one level multidimensional array.
$serialized_array = array_map("serialize", $input);
foreach ($serialized_array as $key => $val) {
$result[$val] = true;
}
$output = array_map("unserialize", (array_keys($result)));
Reference first user contributed note of array_unique() function page in php.net
This solution is relevant only when uniqueness is needed for one array column, for example here if we need the uniqueness in index #0 of the arrays.
Solution #1:
Using array_filter with an anonymous function and a static variable:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$list = array_filter($list, function ($item) {
static $values = [];
if (!in_array($item[0], $values)) {
$values[] = $item[0];
return true;
} else {
return false;
}
});
var_dump($list);
Solution #2:
Since the value when we want the uniqueness are of string type, we can remap the original global array to use these values as keys, which will remove duplicates as we remap it:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$unique = [];
foreach ($list as $item) {
$unique[$item[0]] = $item;
}
// Remap again to integers
$unique = array_values($unique);
var_dump($unique);
An alternative to serialize and unique
$test = [
['abc','def'],
['ghi','jkl'],
['mno','pql'],
['abc','def'],
['ghi','jkl'],
['mno','pql'],
];
$result = array_reduce(
$test,
function($carry,$item){
if(!in_array($item,$carry)) {
array_push($carry,$item);
}
return $carry;
},
[]
);
var_dump($result);
/*
php unique.php
array(3) {
[0] =>
array(2) {
[0] =>
string(3) "abc"
[1] =>
string(3) "def"
}
[1] =>
array(2) {
[0] =>
string(3) "ghi"
[1] =>
string(3) "jkl"
}
[2] =>
array(2) {
[0] =>
string(3) "mno"
[1] =>
string(3) "pql"
}
}
*/
I've given this problem a lot of thought and have determined that the optimal solution should follow two rules.
For scalability, modify the array in place; no copying to a new array
For performance, each comparison should be made only once
With that in mind and given all of PHP's quirks, below is the solution I came up with. Unlike some of the other answers, it has the ability to remove elements based on whatever key(s) you want. The input array is expected to be numeric keys.
$count_array = count($input);
for ($i = 0; $i < $count_array; $i++) {
if (isset($input[$i])) {
for ($j = $i+1; $j < $count_array; $j++) {
if (isset($input[$j])) {
//this is where you do your comparison for dupes
if ($input[$i]['checksum'] == $input[$j]['checksum']) {
unset($input[$j]);
}
}
}
}
}
The only drawback is that the keys are not in order when the iteration completes. This isn't a problem if you're subsequently using only foreach loops, but if you need to use a for loop, you can put $input = array_values($input); after the above to renumber the keys.
try this solution for (n) Dimensional array with non-restricted length
for example this array
$arr= [
0 => [0=>"a" , 1=>"b" , 2=>"c" ] ,
1 => [0=>"x" , 1=>"b" , 2=>"a", 3=>"p"],
2=> [
[
0=>"y" ,
1=>"b" ,
2=> [0=>"x" , 1=>"m" , 2=>"a"]
],
1=>"z" ,
2=>"v"
]
];
This would be the solution
$ar2=[];
$ar3=[];
function test($arr){
global $ar2,$ar3;
if(is_array($arr)){
return array_map("test",$arr);
}
if(!isset($ar2[$arr])){
$ar2[$arr]=1;
$ar3[]=$arr;
}
}
array_map("test",$arr);
print_r($ar3);
Based on the Answer marked as correct, adding my answer. Small code added just to reset the indices-
$input = array_values(array_map("unserialize", array_unique(array_map("serialize", $inputArray))));

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

How can I remove duplicate values from a multi-dimensional array in PHP?
Example array:
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
[3] => Array
(
[0] => abc
[1] => def
)
[4] => Array
(
[0] => ghi
[1] => jkl
)
[5] => Array
(
[0] => mno
[1] => pql
)
)
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)));
Since 5.2.9 you can use array_unique() if you use the SORT_REGULAR flag like so:
array_unique($array, SORT_REGULAR);
This makes the function compare elements for equality as if $a == $b were being used, which is perfect for your case.
Output
Array
(
[0] => Array
(
[0] => abc
[1] => def
)
[1] => Array
(
[0] => ghi
[1] => jkl
)
[2] => Array
(
[0] => mno
[1] => pql
)
)
Keep in mind, though, that the documentation states:
array_unique() is not intended to work on multi dimensional arrays.
I had a similar problem but I found a 100% working solution for it.
<?php
function super_unique($array,$key)
{
$temp_array = [];
foreach ($array as &$v) {
if (!isset($temp_array[$v[$key]]))
$temp_array[$v[$key]] =& $v;
}
$array = array_values($temp_array);
return $array;
}
$arr="";
$arr[0]['id']=0;
$arr[0]['titel']="ABC";
$arr[1]['id']=1;
$arr[1]['titel']="DEF";
$arr[2]['id']=2;
$arr[2]['titel']="ABC";
$arr[3]['id']=3;
$arr[3]['titel']="XYZ";
echo "<pre>";
print_r($arr);
echo "unique*********************<br/>";
print_r(super_unique($arr,'titel'));
?>
Another way. Will preserve keys as well.
function array_unique_multidimensional($input)
{
$serialized = array_map('serialize', $input);
$unique = array_unique($serialized);
return array_intersect_key($input, $unique);
}
Array
(
[0] => Array
(
[id] => 1
[name] => john
)
[1] => Array
(
[id] => 2
[name] => smith
)
[2] => Array
(
[id] => 3
[name] => john
)
[3] => Array
(
[id] => 4
[name] => robert
)
)
$temp = array_unique(array_column($array, 'name'));
$unique_arr = array_intersect_key($array, $temp);
This will remove the duplicate names from array. unique by key
If "remove duplicates" means "remove duplicates, but let one there", a solution might be to apply the array_unique(...) on the "identifier column" first and then to remove in the original array all the keys, that have been removed from the column array:
$array = [
[
'id' => '123',
'foo' => 'aaa',
'bar' => 'bbb'
],
[
'id' => '123',
'foo' => 'ccc',
'bar' => 'ddd'
],
[
'id' => '567',
'foo' => 'eee',
'bar' => 'fff'
]
];
$ids = array_column($array, 'id');
$ids = array_unique($ids);
$array = array_filter($array, function ($key, $value) use ($ids) {
return in_array($value, array_keys($ids));
}, ARRAY_FILTER_USE_BOTH);
The result is:
Array
(
[0] => Array
(
[id] => 123
[foo] => aaa
[bar] => bbb
)
[2] => Array
(
[id] => 567
[foo] => eee
[bar] => fff
)
)
The user comments on the array_unique() documentation have many solutions to this. Here is one of them:
kenrbnsn at rbnsn dot com
27-Sep-2005 12:09
Yet another Array_Unique for multi-demensioned arrays. I've only tested this on two-demensioned arrays, but it could probably be generalized for more, or made to use recursion.
This function uses the serialize, array_unique, and unserialize functions to do the work.
function multi_unique($array) {
foreach ($array as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
This is from http://ca3.php.net/manual/en/function.array-unique.php#57202.
if you need to eliminate duplicates on specific keys, such as a mysqli id, here's a simple funciton
function search_array_compact($data,$key){
$compact = [];
foreach($data as $row){
if(!in_array($row[$key],$compact)){
$compact[] = $row;
}
}
return $compact;
}
Bonus Points
You can pass an array of keys and add an outer foreach, but it will be 2x slower per additional key.
if you have an array like this:
(users is the name of the array)
Array=>
[0] => (array)
'user' => 'john'
'age' => '23'
[1] => (array)
'user' => 'jane'
'age' => '20'
[2]=> (array)
'user' => 'john'
'age' => '23'
and you want to delete duplicates...then:
$serialized = array();
for ($i=0; $i < sizeof($users); $i++) {
$test = in_array($users['user'], $serialized);
if ($test == false) {
$serialized[] = $users['user'];
}
}
can be a solution :P
Lots of person asked me how to make Unique multidimensional array. I have taken reference from your comment and it helps me.
First of All, Thanks to #jeromegamez #daveilers for your solution. But every time i gave the answer, they asked me how this 'serialize' and 'unserialize' works. That's why i want to share the reason of this with you so that it will help more people to understand the concept behind this.
I am explaining why we use 'serialize' and 'unserialize' in steps :
Step 1: Convert the multidimensional array to one-dimensional array
To convert the multidimensional array to a one-dimensional array, first generate byte stream representation of all the elements (including nested arrays) inside the array. serialize() function can generate byte stream representation of a value. To generate byte stream representation of all the elements, call serialize() function inside array_map() function as a callback function. The result will be a one dimensional array no matter how many levels the multidimensional array has.
Step 2: Make the values unique
To make this one dimensional array unique, use array_unique() function.
Step 3: Revert it to the multidimensional array
Though the array is now unique, the values looks like byte stream representation. To revert it back to the multidimensional array, use unserialize() function.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Thanks again for all this.
A very easy and logical way to Unique a multi dimension array is as follows,
If you have array like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value1
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value1
[3] => Value3
[4] => Value4
)
)
use foreach to solve this:
foreach($array as $k=>$v){
$unique=array_unique($v);
$array[$k]=$unique;
}
it will give you following result:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[3] => Value3
[4] => Value4
)
)
and if you want to rearrange the order of the keys,
foreach($array as $k=>$v){
$unique= array_values(array_unique($v));
$array[$k]=$unique;
}
This operation will give you arranged key values like this:
Array
(
[Key1] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
)
[Key2] => Array
(
[0] => Value1
[1] => Value2
[2] => Value3
[3] => Value4
)
)
I hope this will clear everything.
An easy to read solution, probably not the most efficient:
function arrayUnique($myArray){
if(!is_array($myArray))
return $myArray;
foreach ($myArray as &$myvalue){
$myvalue=serialize($myvalue);
}
$myArray=array_unique($myArray);
foreach ($myArray as &$myvalue){
$myvalue=unserialize($myvalue);
}
return $myArray;
}
As people are saying array_unique() is very slow, here is a snippet I use for one level multidimensional array.
$serialized_array = array_map("serialize", $input);
foreach ($serialized_array as $key => $val) {
$result[$val] = true;
}
$output = array_map("unserialize", (array_keys($result)));
Reference first user contributed note of array_unique() function page in php.net
This solution is relevant only when uniqueness is needed for one array column, for example here if we need the uniqueness in index #0 of the arrays.
Solution #1:
Using array_filter with an anonymous function and a static variable:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$list = array_filter($list, function ($item) {
static $values = [];
if (!in_array($item[0], $values)) {
$values[] = $item[0];
return true;
} else {
return false;
}
});
var_dump($list);
Solution #2:
Since the value when we want the uniqueness are of string type, we can remap the original global array to use these values as keys, which will remove duplicates as we remap it:
<?php
$list = [
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql'],
['abc', 'def'],
['ghi', 'jkl'],
['mno', 'pql']
];
$unique = [];
foreach ($list as $item) {
$unique[$item[0]] = $item;
}
// Remap again to integers
$unique = array_values($unique);
var_dump($unique);
An alternative to serialize and unique
$test = [
['abc','def'],
['ghi','jkl'],
['mno','pql'],
['abc','def'],
['ghi','jkl'],
['mno','pql'],
];
$result = array_reduce(
$test,
function($carry,$item){
if(!in_array($item,$carry)) {
array_push($carry,$item);
}
return $carry;
},
[]
);
var_dump($result);
/*
php unique.php
array(3) {
[0] =>
array(2) {
[0] =>
string(3) "abc"
[1] =>
string(3) "def"
}
[1] =>
array(2) {
[0] =>
string(3) "ghi"
[1] =>
string(3) "jkl"
}
[2] =>
array(2) {
[0] =>
string(3) "mno"
[1] =>
string(3) "pql"
}
}
*/
I've given this problem a lot of thought and have determined that the optimal solution should follow two rules.
For scalability, modify the array in place; no copying to a new array
For performance, each comparison should be made only once
With that in mind and given all of PHP's quirks, below is the solution I came up with. Unlike some of the other answers, it has the ability to remove elements based on whatever key(s) you want. The input array is expected to be numeric keys.
$count_array = count($input);
for ($i = 0; $i < $count_array; $i++) {
if (isset($input[$i])) {
for ($j = $i+1; $j < $count_array; $j++) {
if (isset($input[$j])) {
//this is where you do your comparison for dupes
if ($input[$i]['checksum'] == $input[$j]['checksum']) {
unset($input[$j]);
}
}
}
}
}
The only drawback is that the keys are not in order when the iteration completes. This isn't a problem if you're subsequently using only foreach loops, but if you need to use a for loop, you can put $input = array_values($input); after the above to renumber the keys.
try this solution for (n) Dimensional array with non-restricted length
for example this array
$arr= [
0 => [0=>"a" , 1=>"b" , 2=>"c" ] ,
1 => [0=>"x" , 1=>"b" , 2=>"a", 3=>"p"],
2=> [
[
0=>"y" ,
1=>"b" ,
2=> [0=>"x" , 1=>"m" , 2=>"a"]
],
1=>"z" ,
2=>"v"
]
];
This would be the solution
$ar2=[];
$ar3=[];
function test($arr){
global $ar2,$ar3;
if(is_array($arr)){
return array_map("test",$arr);
}
if(!isset($ar2[$arr])){
$ar2[$arr]=1;
$ar3[]=$arr;
}
}
array_map("test",$arr);
print_r($ar3);
Based on the Answer marked as correct, adding my answer. Small code added just to reset the indices-
$input = array_values(array_map("unserialize", array_unique(array_map("serialize", $inputArray))));

Categories