How to remove duplicate values from a multi-dimensional array in PHP

How to remove duplicate values from a multi-dimensional array in PHP

Whenever you need to remove duplicate values from simple array or multi-dimensional array in PHP and you can follow with given code to get unique values in array.

array_unique() function are used to remove duplicate data from given array. Now question is what value will be removed if there are two or more array values are same, in that scenario it will keep first appearance value in array and removed others.

There are two parameters which will be pass in array_unique(), first specifying an array which is required and second is optioanl specifying sorting type.

Optional parameters sortingtype have possible values :

  • SORT_STRING - Default
  • SORT_NUMERIC
  • SORT_REGULAR
  • SORT_LOCALE_STRING

Remove duplicate values from an array in PHP

Have a look on given example to remove duplicate values from an array.

  1. <?php
  2. $a=array("a"=>"expertphp","b"=>"demo","c"=>"expertphp");
  3. echo "<pre>";
  4. print_r(array_unique($a));
  5. ?>

Output of array before removing duplicate values from a multi-dimensional array in PHP.

  1. Array
  2. (
  3. [a] => expertphp
  4. [b] => demo
  5. [c] => expertphp
  6. )

Output of array after removing duplicate values from an array in PHP.

  1. Array
  2. (
  3. [a] => expertphp
  4. [b] => demo
  5. )

Now you will know how to remove duplicate values from a multi-dimensional array.

Have a look on given example to remove duplicate values from a multi-dimensional array.

  1. <?php
  2. $input=Array(Array('ab','cd'),Array('ef','gh'),Array('ij','kl'),Array('ab','cd'));
  3. echo "<pre>";
  4. print_r($input);
  5. $input = array_map("unserialize", array_unique(array_map("serialize", $input)));
  6. print_r($input);
  7. ?>

Output of array before removing duplicate values from a multi-dimensional array in PHP.

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [0] => ab
  6. [1] => cd
  7. )
  8. [1] => Array
  9. (
  10. [0] => ef
  11. [1] => gh
  12. )
  13. [2] => Array
  14. (
  15. [0] => ij
  16. [1] => kl
  17. )
  18. [3] => Array
  19. (
  20. [0] => ab
  21. [1] => cd
  22. )
  23. )

Output of array after removing duplicate values from a multi-dimensional array in PHP.

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [0] => ab
  6. [1] => cd
  7. )
  8. [1] => Array
  9. (
  10. [0] => ef
  11. [1] => gh
  12. )
  13. [2] => Array
  14. (
  15. [0] => ij
  16. [1] => kl
  17. )
  18. )

Find the Duplicate values in array using PHP

  1. <?php
  2. $array=array("expertphp","demo","expertphp");
  3. $count_array = array_count_values($array);
  4. echo "<pre>";
  5. print_r($count_array);
  6. $results = array();
  7. foreach($count_array as $key=>$val){
  8. if($val == 1){
  9. $results[$key] = 'unique';
  10. }
  11. else{
  12. $results[$key] = 'duplicate';
  13. }
  14. }
  15. echo "<pre>";
  16. print_r($results);
  17. ?>

Output :

  1. Array
  2. (
  3. [expertphp] => 2
  4. [demo] => 1
  5. )
  6. Array
  7. (
  8. [expertphp] => duplicate
  9. [demo] => unique
  10. )

Phone: (+91) 8800417876
Noida, 201301