How to emove duplicate values from PHP Array? explain with example

PHP array_unique

PHP array_unique() function to remove duplicate elements or values from an array. If the array contains the string keys, then this function will keep the first key encountered for every value, and ignore all the subsequent keys.


   $array = array("a" => "One", "Tow", "One" => "Tow", "Three", "Four");
   // Deleting the duplicate items
   $result = array_unique($array);


Removing duplicate values from an array in PHP

To remove duplicate values from an array in PHP, use the array_unique() function. If two or more array values are the same, the first appearance will be kept, and the other will be removed. The returned array will hold the first array item’s key type.


array_unique(array, [sortingType])


The array parameter is required, which specifies the array.

sortingType parameter is optional, and it specifies how to compare the array element. The following are some soringType flags.

  1. SORT_REGULAR – compare items usually (don’t change types)
  2. SORT_NUMERIC – compare items numerically
  3. SORT_STRING – compare items as strings
  4. SORT_LOCAL_STRING – compare items as strings based on the current locale.



   $data = [20, 21, 21, 22, 21, 23, 22, 46, 47];




Associative Array in PHP array_unique() Function

Let’s pass the associative array to the array_unique function and see the output.



    $data = ['a' => 'Vijay', 
        'b' => 'Amit',
        'd' => 'Vijay', 
        'f' => 'Amit', 
        'c' => 'Rahul', 
        'k' => 'Roshan', 
        'n' => 'Vijay'];
    $output = array_unique($data);



Create a multidimensional array unique

You can create a multidimensional array unique for any single key index. 



    function unique_multi_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 
    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
    return $temp_array; 
  $data = array( 
    0 => array("id"=>"1", "name"=>"Vijay",  "age"=>"26"), 
    1 => array("id"=>"2", "name"=>"Amit", "age"=>"25"), 
    2 => array("id"=>"1", "name"=>"Rahul",  "age"=>"26"), 
  $output = unique_multi_array($data,'id');



Related Posts

PHP – strip_tags() Replace with Spaces Example

We are going to learn php strip tags with spaces. You will learn how to replace php Strip_tags with space. In this article, we will replace php…

How to Get Duplicate Values from Array in PHP?

We will learn php array get duplicate values. we will help you to give an example of how to get duplicate values from array in php. you…

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

How Can I Convert a String in PHP Into an Array?

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x