Remove Duplicate values from an Array in PHP

The array_unique() method is used to remove repeated or duplicate values from the array and return an array.

It is better to use this method on the array if there is some possibility of the same values or the later code in the program depends on it for example – selecting records based on ids and displaying them on the screen.

In this tutorial, I show how you can remove duplicate values from –

  • An Indexed,
  • Associative array and
  • Remove index by key name in the associative array.

Remove Duplicate values from an Array in PHP


Contents

  1. Remove duplicate value from Indexed Array
  2. Remove duplicate value from Associative Array
  3. Remove by key from Associative Array
  4. Conclusion

1. Remove duplicate value from Indexed Array

Define an array $num_arr where I also added some duplicate numbers.

Pass the array in array_unique() method to remove duplicate values.

Example

<?php

// Numberic array
$num_arr = array(1,2,3,1,6,1,23,2);

// Remove duplicate values
$num_unique = array_unique($num_arr);

echo "<pre>";
print_r($num_arr);
echo "</pre>";

echo "<b>After remove duplicate</b>";
echo "<pre>";
print_r($num_unique);
echo "</pre>";

Output

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 1
    [4] => 6
    [5] => 1
    [6] => 23
    [7] => 2
)
After remove duplicate
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [4] => 6
    [6] => 23
)

2. Remove duplicate value from Associative Array

Define an associative array $student_arr with name and age keys.

By calling array_unique() method it will remove those indexes which have name and age key values that are common.

NOTE – I passed SORT_REGULAR as second parameter in array_unique() method.

Example

<?php
$student_arr[] = array("name" => "Yogesh Singh","age"=>24);
$student_arr[] = array("name" => "Sonarika Bhadoria","age"=>24);
$student_arr[] = array("name" => "Anil Singh","age" => 23);
$student_arr[] = array("name" => "Yogesh Singh","age" => 24);

$student_arr1 = array_unique($student_arr,SORT_REGULAR);

echo "<pre>";
print_r($student_arr);
echo "</pre>";

echo "<b>After remove duplicate</b>";
echo "<pre>";
print_r($student_arr1);
echo "</pre>";

Output

Array
(
    [0] => Array
        (
            [name] => Yogesh Singh
            [age] => 24
        )
    [1] => Array
        (
            [name] => Sonarika Bhadoria
            [age] => 24
        )
    [2] => Array
        (
            [name] => Anil Singh
            [age] => 23
        )
    [3] => Array
        (
            [name] => Yogesh Singh
            [age] => 24
        )
)
After remove duplicate
Array
(
    [0] => Array
        (
            [name] => Yogesh Singh
            [age] => 24
        )
    [1] => Array
        (
            [name] => Sonarika Bhadoria
            [age] => 24
        )
    [2] => Array
        (
            [name] => Anil Singh
            [age] => 23
        )
)

3. Remove by key from Associative Array

Define an associative array $student_arr.

Create unique_key() function which takes an array and key name by which remove duplicate indexes.

In this function loop on the $array and check if $keyname isset on the $new_array or not. If not set then assign $value in $new_array[$value[$keyname]].

Reindex the $new_array with array_values() and return it.

Example

<?php
function unique_key($array,$keyname){

 $new_array = array();
 foreach($array as $key=>$value){

   if(!isset($new_array[$value[$keyname]])){
     $new_array[$value[$keyname]] = $value;
   }

 }
 $new_array = array_values($new_array);
 return $new_array;
}

// Array
$student_arr[] = array("name" => "Yogesh Singh","age"=>24);
$student_arr[] = array("name" => "Sonarika Bhadoria","age"=>24);
$student_arr[] = array("name" => "Anil Singh","age" => 23);
$student_arr[] = array("name" => "Mayank Patidar","age" => 25);
$student_arr[] = array("name" => "Anil Singh","age" => 19);

// Remove duplicate value according to 'name'
$student_unique_arr = unique_key($student_arr,'name');

echo "<pre>";
print_r($student_arr);
echo "</pre>";

echo "<b>Array after remove duplicate key</b>";
echo "<pre>";
print_r($student_unique_arr);
echo "</pre>";

Output

Array
(
    [0] => Array
        (
            [name] => Yogesh Singh
            [age] => 24
        )
    [1] => Array
        (
            [name] => Sonarika Bhadoria
            [age] => 24
        )
    [2] => Array
        (
            [name] => Anil Singh
            [age] => 23
        )
    [3] => Array
        (
            [name] => Mayank Patidar
            [age] => 25
        )
    [4] => Array
        (
            [name] => Anil Singh
            [age] => 19
        )
)
Array after remove duplicate key
Array
(
    [0] => Array
        (
            [name] => Yogesh Singh
            [age] => 24
        )
    [1] => Array
        (
            [name] => Sonarika Bhadoria
            [age] => 24
        )
    [2] => Array
        (
            [name] => Anil Singh
            [age] => 23
        )
    [3] => Array
        (
            [name] => Mayank Patidar
            [age] => 25
        )
)

4. Conclusion

Pass your array in the array_unique() method to remove common values it works with both indexed and associative array.

If you like to remove array indexes according to key name in associative then you can use above-created unique_key() function and customize it according to your preference.

If you found this tutorial helpful then don't forget to share.

1 thought on “Remove Duplicate values from an Array in PHP”

  1. how to remove first duplicate key not second key using unique_key()
    ex :
    [0] => Array
    (
    [name] => Singh
    [age] => 10
    )
    [1] => Array
    (
    [name] => Singh
    [age] => 20
    )
    required output :
    [1] => Array
    (
    [name] => Singh
    [age] => 20
    )

    Reply

Leave a Comment