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)));
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
PHP remove duplicate arrays inside a multi dimensional array
No function does this outright, you could use a combination of functions though. You could first sort all of sub batches of array first into ascending order first, then serialize each on them, utilize array_unique
, then unserialize again to have that multi dimensional again:
foreach($arr as &$a){ sort($a); }
$arr = array_map('unserialize', array_unique(array_map('serialize', $arr)));
print_r($arr);
PHP remove duplicate values from multidimensional array
The user comments on the array_unique page do shed some light on this. You will most likely find some hidden gems in those comments - its a very handy documentation.
Just a quick browser through revealed the following to remove duplicates from a multi dimensional array:
<?php
function super_unique($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = super_unique($value);
}
}
return $result;
}
?>
php multi-dimensional array remove duplicate
A quick solution which uses the uniqueness of array indexes:
$newArr = array();
foreach ($array as $val) {
$newArr[$val[2]] = $val;
}
$array = array_values($newArr);
Notice 1: As visible from above, the last match for an email address is used instead of the first. This can be changed by replacing the second line with
foreach (array_reverse($array) as $val) {
Notice 2: The indexes in the resulting array are somewhat mixed up. But I guess this doesn't matter...
Remove duplicate values from a multidimensional array
I'll put this at the beginning, because I'd rather do this type of thing in the database if possible. You can change your query to get a result like the one you want.
SELECT name, MIN(id) FROM your_table GROUP BY name
If you need to do this in PHP for some other reason, here are a few different ways.
All of these involve assigning the row to your result array using the name as the key.
If you want to keep the first instance of each name, then as you iterate your input array, if a key matching the name of the row exists in the result array, don't add it. This way you'll end up with the first instance of the person's name in your result set.
foreach ($your_array as $person) {
if (!isset($unique[$person['name']]) {
$unique[$person['name']] = $person;
}
}
$unique = array_values($unique);
If it doesn't matter which ID you get, then you can leave out the isset
check. This way you'll end up with the last instance of each person's name in your result set because that key will be overwritten each time you encounter a duplicate.
foreach ($your_array as $person) {
$unique[$person['name']] = $person;
}
$unique = array_values($unique);
If you need to keep all the ids associated with a name, append the id value to an array under the name key instead of adding the entire row.
foreach ($your_array as $person) {
$people[$person['name']][] = $person['id];
}
This will give you a result like
['John' => ['D', 'U'], 'Grace' => ['K']];
Remove duplicates from multi dimentional array which include string and laravel collection
You can use Laravel's Collection::unique
method:
$unique = collect($yourArray)->unique('email');
By passing in email
into the method, you're telling Laravel to look at that specific field in your dataset, rather than the data as a whole. You can then convert it back to an array using toArray
.
$unique = collect($yourArray)->unique('email')->toArray();
You can also pass a closure to the unique
method to allow you to define the value that you want to compare for each object to determine uniqueness:
$unique = collect($yourArray)->unique(function ($item) {
return $item['email'];
});
Related Topics
PHPexcel Runs Out of 256, 512 and Also 1024Mb of Ram
PHP Multi-Dimensional Array Remove Duplicate
Nginx Location Configuration (Subfolders)
Measuring the Distance Between Two Coordinates in PHP
Build Select Query With Dynamic Number of Like Conditions as a MySQLi Prepared Statement
How to Create an Array from a CSV File Using PHP and the Fgetcsv Function
How to Remove Accents and Turn Letters into "Plain" Ascii Characters
How to Initialize Static Variables
How to Check Whether Mod_Rewrite Is Enable on Server
How to Get Directory Size in PHP
Reference Assignment Operator in PHP, =&
How to Truncate a String to the First 20 Words in PHP
Pdo::_Construct(): Server Sent Charset (255) Unknown to the Client. Please, Report to the Developers
PHP Fatal Error: Cannot Redeclare Class
Remove Index.PHP from Url - Codeigniter 2
How to Handle the Warning of File_Get_Contents() Function in PHP