How can i remove duplicate values from a multi dimensional array in php?
How can I remove duplicate values from a multi-dimensional array in PHP? Show Example array:
asked Nov 21, 2008 at 2:50
1 Here is another way. No intermediate variables are saved. We used this to de-duplicate results from a variety of overlapping queries.
jeromegamez 3,1521 gold badge22 silver badges36 bronze badges answered Jun 3, 2009 at 18:12
daveilersdaveilers 7,4261 gold badge16 silver badges4 bronze badges 13 Since 5.2.9 you can use
This makes the function compare elements for equality as if Output
Keep in mind, though, that the documentation states:
answered Aug 22, 2013 at 7:01
Ja͢ckJa͢ck 168k36 gold badges255 silver badges305 bronze badges 14
I had a similar problem but I found a 100% working solution for it.
Martin 21.2k8 gold badges62 silver badges117 bronze badges answered May 9, 2012 at 10:48
3 Another way. Will preserve keys as well.
answered Nov 21, 2008 at 14:36
OISOIS 9,5921 gold badge31 silver badges41 bronze badges 1
This will remove the duplicate names from array. unique by key
answered Feb 23, 2018 at 6:34
Mahak ChoudharyMahak Choudhary 1,0661 gold badge15 silver badges13 bronze badges 2 If "remove duplicates" means "remove duplicates, but let one there", a solution might be to
apply the
The result is:
answered May 20, 2016 at 22:14
automatixautomatix 13k24 gold badges97 silver badges218 bronze badges 1 The user comments on the array_unique() documentation have many solutions to this. Here is one of them:
This is from http://ca3.php.net/manual/en/function.array-unique.php#57202.
Manoj Sharma 1,4672 gold badges13 silver badges20 bronze badges answered Nov 21, 2008 at 3:07
Paige RutenPaige Ruten 167k36 gold badges175 silver badges195 bronze badges 0 if you need to eliminate duplicates on specific keys, such as a mysqli id, here's a simple funciton
Bonus Points You can pass an array of keys and add an outer foreach, but it will be 2x slower per additional key.
answered Nov 8, 2014 at 7:17
r3wtr3wt 4,5722 gold badges31 silver badges55 bronze badges 2 if you have an array like this: (users is the name of the array)
and you want to delete duplicates...then:
can be a solution :P answered Aug 7, 2015 at 18:45
LimonLimon 1,7226 gold badges31 silver badges58 bronze badges 1 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.
Thanks again for all this. answered Sep 1, 2016 at 11:29
ManishManish 3,2131 gold badge20 silver badges24 bronze badges A very easy and logical way to Unique a multi dimension array is as follows, If you have array like this:
use
it will give you following result:
and if you want to rearrange the order of the keys,
This operation will give you arranged key values like this:
I hope this will clear everything. answered Jan 19, 2018 at 13:22
1 An easy to read solution, probably not the most efficient:
graham.reeds 15.9k17 gold badges70 silver badges133 bronze badges answered Jun 19, 2009 at 19:44
pixelinepixeline 17.5k11 gold badges80 silver badges109 bronze badges As people are saying
Reference first user contributed note of answered Apr 18, 2015 at 19:01
AnujAnuj 3234 silver badges13 bronze badges 0 An alternative to serialize and unique
*/ answered Nov 6, 2014 at 23:04
1 I've given this problem a lot of thought and have determined that the optimal solution should follow two rules.
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.
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 answered Mar 5, 2015 at 14:04
SnakeSnake 811 silver badge4 bronze badges 1 try this solution for (n) Dimensional array with non-restricted length
This would be the solution
answered May 26 at 13:44
Based on the Answer marked as correct, adding my answer. Small code added just to reset the indices-
answered Feb 14, 2020 at 11:01
GaganGagan 2942 silver badges15 bronze badges 1 How do you remove duplicates from an array in PHP?The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed.
How can we find duplicate values in multidimensional array in PHP?“check duplicate values in multidimensional array php” Code Answer's. $counts = array_count_values($array);. $duplicate_title = array_filter($array, function ($value) use ($counts) {. return $counts[$value] > 1;. How do you remove duplicates from an array of arrays?To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.
How do you find duplicate elements in a multidimensional array?Turn the 2d array into a 1d array ( List
|