How to remove duplicate values from an array in PHP
Today, we’ll explain to you how to remove duplicate values from an array in PHP. In this short article, we will look at different types of an array with examples.
Using php array_unique() function we can easily remove duplicate values and get a unique array.
Remove Duplicate Values from:
Let’s take an example for better understanding.
1. Numerical Array
Here, we’ll take a numerical array with duplicate numbers and use the array_unique()
function to remove duplicate values from an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $num_array = array(2,4,5,43,5,8,5,32); $unique_array = array_unique($num_array); print_r($unique_array); ?> // Output: Array ( [0] => 2 [1] => 4 [2] => 5 [3] => 43 [5] => 8 [7] => 32 ) |
You can see in the above output array which is not reindexed after removing the duplicate element from an array. So we can use the array_values() functions to get a re-indexed array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $result = array_values($unique_array); print_r($result); ?> // Output: Array ( [0] => 2 [1] => 4 [2] => 5 [3] => 43 [4] => 8 [5] => 32 ) |
2. Associative Array
Now, we will take a student array which has the student name and marks. The array_unique()
function will remove those elements which have the same student name and marks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php $students = array( array( 'name' => 'Denny', 'marks' => 89 ), array( 'name' => 'John', 'marks' => 80 ), array( 'name' => 'Aien', 'marks' => 93 ), array( 'name' => 'Denny', 'marks' => 89 ) ); $unique_array = array_unique($students,SORT_REGULAR); print_r($unique_array); ?> // Output: Array ( [0] => Array ( [name] => Denny [marks] => 89 ) [1] => Array ( [name] => John [marks] => 80 ) [2] => Array ( [name] => Aien [marks] => 93 ) ) |
3. Remove by Key name from Associative Array
Here, we’ll create a custom function to remove duplicate values by key name from the associative array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php function unique_array($array,$keyname){ $result = array(); foreach ($array as $key => $val) { // check $keyname set in $result array or not if (!isset($result[$val[$keyname]])){ // if not set then assign $val to $result array $result[$val[$keyname]] = $val; } } // Re-indexed the array $result = array_values($result); // return array return $result; } $students = array( array( 'name' => 'Denny', 'marks' => 89 ), array( 'name' => 'John', 'marks' => 80 ), array( 'name' => 'Aien', 'marks' => 93 ), array( 'name' => 'Denny', 'marks' => 76 ) ); $unique_array = unique_array($students,'name'); print_r($unique_array); ?> // output: Array ( [0] => Array ( [name] => Denny [marks] => 89 ) [1] => Array ( [name] => John [marks] => 80 ) [2] => Array ( [name] => Aien [marks] => 93 ) ) |
That’s it for today.
Thank you for reading. Happy Coding..!!