AppDividend
Latest Code Tutorials

PHP array_unique: Remove Duplicate Values from an Array

Two elements are considered equal if and only if (string) $item1 === (string) $item2. For example, the first element will be used when the string representation is the same.

PHP array_unique

PHP array_unique() is a built-in function which returns an array without duplicate values. The array_unique() method removes duplicate values from an array. It takes an input array and returns a new array without duplicate values.

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.

Syntax

The syntax of array_unique() function is following.

array_unique(array, [sortingType])

Arguments

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.

Example

See the following example.

<?php

// app.php

$data = [19, 21, 19, 21, 46, 21, 29, 21, 18];
print_r(array_unique($data));

So, we have defined the repeated values array, and now we pass that array to the array_unique() function. The output is following.

Remove Duplicate Values From An Array In PHP

The above output is unsorted.

Associative Array in PHP array_unique() Function

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

<?php

// app.php

$data = ['a' => 'krunal', 
        'b' => 'ankit',
        'd' => 'khushbu', 
        'f' => 'nehal', 
        'c' => 'rushabh', 
        'k' => 'krunal', 
        'n' => 'nehal'];
$output = array_unique($data);
print_r($output);

The output is following.

Associative Array in PHP array_unique() Function

Here, one thing to note is that array_unique() is not intended to work on multidimensional arrays.

Create a multidimensional array unique

You can create a multidimensional array unique for any single key index. See the following code.

<?php

function unique_multidim_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; 
      } 
      $i++; 
  } 
  return $temp_array; 
}

So, we have created a function that removes the values from the multidimensional array whose key has the same values.

We are taking an argument of a multidimensional array and key.

In the above function, the key is an important parameter because the key’s value determines whether the array values stay in an array or not. In addition, it decides if it is duplicated or not.

See the full code with the example below.

<?php

// app.php

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; 
      } 
      $i++; 
  } 
  return $temp_array; 
}

$data = array( 
  0 => array("id"=>"1", "name"=>"Krunal",  "age"=>"26"), 
  1 => array("id"=>"2", "name"=>"Ankit", "age"=>"25"), 
  2 => array("id"=>"1", "name"=>"Krunal",  "age"=>"26"), 
); 
$output = unique_multi_array($data,'id');
print_r($output);

The output is following.

How To Remove Duplicate Values From An Array In PHP

That’s it for this tutorial.

2 Comments
  1. Irwan Maulana says

    Thanks Brother, That Work (:

  2. Praveen kumar says

    Remove Duplicate Values From An Array In PHP is over.
    using for loop

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.