AppDividend
Latest Code Tutorials

How To Remove Duplicate Values From An Array In PHP

1

In this tutorial, we will see How To Remove Duplicate Values From An Array In PHP. PHP language has in-built called array_unique() which returns an array without duplicate values. PHP array_unique() method removes duplicate values from an array. It takes an input array and returns a new array without duplicate values. Two elements are considered equal if and only if (string) $item1 === (string) $item2 for example when the string representation is the same, the first element will be used.

Remove Duplicate Values From An Array In PHP

 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.

The syntax of array_unique() function is following.

array_unique(array, [sortingType])

The array parameter is required which specified 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.

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 that array_unique() is not intended to work on multidimensional arrays.

Create 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 in which, we are removing 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. It decides if it is duplicate 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

Finally, How To Remove Duplicate Values From An Array In PHP is over.

1 Comment
  1. Irwan Maulana says

    Thanks Brother, That Work (:

Leave A Reply

Your email address will not be published.

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