Home > Backend Development > PHP Tutorial > How to determine and remove duplicate data in an array in PHP

How to determine and remove duplicate data in an array in PHP

WBOY
Release: 2016-07-25 08:58:58
Original
1062 people have browsed it
  1. if (count($array) != count(array_unique($array))) {
  2. echo 'The array has duplicate values';
  3. }
  4. ?>
Copy code

PHP remove duplicate array data

  1. $input = array("a" => "green","", "red","b" => "green", "" ,"blue", "red","c" => "witer","hello","witer");

  2. //$result = array_unique($input); //Remove duplicate elements
  3. $result = a_array_unique ($input); //Leave only a single element
  4. foreach($result as $aa)
  5. {
  6. echo $aa."
    ";
  7. }
  8. function multi_unique($array) {
  9. foreach ( $array as $k=>$na)
  10. $new[$k] = serialize($na);
  11. $uniq = array_unique($new);
  12. foreach($uniq as $k=>$ser)
  13. $new1[$k] = unserialize($ser);
  14. return ($new1);
  15. }

  16. function a_array_unique($array)//Written better

  17. {
  18. $out = array();
  19. foreach ($array as $key=>$value) {
  20. if (!in_array($value, $out))
  21. {
  22. $out[$key] = $value;
  23. }
  24. }
  25. return $out;
  26. }
  27. ?>
Copy code

PHP array has a built-in function array_unique () to remove duplicate items, but PHP's array_unique function only applies to one-dimensional arrays. Not suitable for multi-dimensional arrays. The following implements the array_unique function of a two-dimensional array:

  1. function unique_arr($array2D,$stkeep=false,$ndformat=true)
  2. {
  3. // Determine whether to retain the first-level array key (the first-level array key can be non-numeric)
  4. if($stkeep) $stArr = array_keys($array2D);
  5. // Determine whether to retain the secondary array keys (all secondary array keys must be the same)
  6. if($ndformat) $ndArr = array_keys(end($array2D)) ;
  7. //Dimensionality reduction, you can also use implode to convert a one-dimensional array into a string connected with commas
  8. foreach ($array2D as $v){
  9. $v = join(",",$v);
  10. $ temp[] = $v;
  11. }
  12. //Remove repeated strings, that is, repeated one-dimensional arrays
  13. $temp = array_unique($temp);
  14. //Reassemble the disassembled array
  15. foreach ($ temp as $k => $v)
  16. {
  17. if($stkeep) $k = $stArr[$k];
  18. if($ndformat)
  19. {
  20. $tempArr = explode(",",$v);
  21. foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
  22. }
  23. else $output[$k] = explode(",",$ v);
  24. }
  25. return $output;
  26. }
  27. ?>
Copy code

Test case:

  1. $array2D = array('first'=>array('title'=>'1111','date'=>'2222'),'second'=> ;array('title'=>'1111','date'=>'2222'),'third'=>array('title'=>'2222','date'=>'3333 '));
  2. print_r($array2D);
  3. print_r(unique_arr($array2D,true));
  4. ?>
Copy code

php determines whether the same value exists in the array array_unique 1 2 times One last page



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template