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 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;
}
?>
How to remove duplicate key value array in multidimensional array
PHP code demo
<?php
$appointments = array(
array('1','Monday', '5:30 PM'),
array('2','Friday', '6:00 PM'),
array('3','Monday', '5:30 PM'),
array('4','Tuesday', '4:15 PM'),
array('5','Wednesday', '8:30 PM'),
array('6','Thursday', '1:45 PM')
);
//Here we are finding column of week days.
$implodedColumns= array_map(function($data){
unset($data[0]);
return implode(",", array_map('trim', $data));
}, $appointments);
$result=array_unique($implodedColumns);
array_walk($result, function(&$value,$key){
$value= array_merge(array($key), explode(",", $value));
});
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => 0
[1] => Monday
[2] => 5:30 PM
)
[1] => Array
(
[0] => 1
[1] => Friday
[2] => 6:00 PM
)
[3] => Array
(
[0] => 3
[1] => Tuesday
[2] => 4:15 PM
)
[4] => Array
(
[0] => 4
[1] => Wednesday
[2] => 8:30 PM
)
[5] => Array
(
[0] => 5
[1] => Thursday
[2] => 1:45 PM
)
)
PHP remove duplicate values from a multidimensional array
You can sort by weight
ascending and then create a result using the name
as the key so the ones with larger weight
will overwrite the smaller weight
ones:
array_multisort(array_column($array, 'weight'), SORT_ASC, $array);
foreach($array as $v) { $result[$v['name']] = $v; }
Then if you want to re-index (not required):
$result = array_values($result);
Remove duplicate values of specific key in multidimensional array in PHP but remove the last?
You can try like the code below:
$my_arrays = array_unique($my_arrays, SORT_REGULAR);
var_dump($my_arrays);
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']];
Related Topics
Laravel - HTMLspecialchars() Expects Parameter 1 to Be String, Object Given
How to Create Codeigniter Batch Insert Array
Fatal Error: Using $This When Not in Object Context
You Must Enable the Openssl Extension to Download Files via Https
Bounce Email Handling with PHP
How to Copy a File from One Directory to Another Using PHP
Dynamic Paypal Button Encryption
Is Making Asynchronous Http Requests Possible with PHP
How to Store the Data in Unicode in Hindi Language
How to Display Image from Database Using PHP
Utf-8 Encoded HTML Pages Show Questions Marks Instead of Characters
Unexpected Character in Input: '\' (Ascii=92) State=1
PHP Objects VS Arrays -- Performance Comparison While Iterating
Dyld: Library Not Loaded: /Usr/Local/Lib/Libpng16.16.Dylib with Anything PHP Related