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;
}
?>
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...
How to remove duplicate arrays from a multi dimensional array?
This is a perfect case for array_uunique()
! No wait, scratch that. The PHP devs refused to implement it for the perfectly valid reason of... [shuffles notes] "the function name looks like a typo".
[sets notes on fire]
Anyhow, you just need to iterate over that data, keep track of the IDs you've seen, and remove entries that you've already seen.
$seen = [];
foreach(array_keys($data) as $i) {
foreach(array_keys($data[$i]) as $j) {
$id = $data[$i][$j]['id'];
if( in_array($id, $seen) ) {
unset($data[$i][$j]);
} else {
$seen[] = $id;
}
}
}
I've opted for the foreach(array_keys(...) as $x)
approach as avoiding PHP references is always the sane choice.
Run it.
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
Session Destroy When One Browser Tab Gets Closed
Internal Server Error While Run Laravel on Localhost
How to Insert Md5 Hash Value in Ms SQL Server
In Laravel,How to Use Foreach Loop
Laravel Eloquent Get Results Grouped by Days
Php: How to Detect If a Session Has Expired Automatically
How to Use a Findby Method With Comparative Criteria
How Remove the Backslash ("\") in the Json Response Using PHP
How to Get the Domain Name Without Www, Subdomain, and Com/Net/Org/Etc
Remove/Hide Woocommerce Added to Cart Message But Keep/Display Coupon Applied Message
Laravel 5 - How to Access Image Uploaded in Storage Within View
Undefined Index: User_Id. Where, User_Id Is Session Variable
Convert HTML Form Data into a Pdf File Using PHP
How to Make a Checkbox Checked and Disabled
How to Get the Sucess Message in the Same Page After Submitting the Contact Form
How to Resolve MySQL Port 3306 Error on Wamp
Check If Username and Email Already Exists in Database
Laravel Eloquent How to Get the Minimum Value That Is Not Null