Two examples of removing duplicate data from arrays in PHP

WBOY
Release: 2016-07-25 08:55:37
Original
924 people have browsed it
  1. /**

  2. * Remove duplicate data from array
  3. * by bbs.it-home.org
  4. **/
  5. $input = array("a" => "green","", "red","b" = > "green", "","blue", "red","c" => "witer","hello","witer");
  6. //$result = array_unique($input); // Remove duplicate elements
  7. $result = a_array_unique($input); //Leave only a single element
  8. foreach($result as $aa)
  9. {
  10. echo $aa."
    ";
  11. }
  12. function multi_unique ($array) {
  13. foreach ($array as $k=>$na)
  14. $new[$k] = serialize($na);
  15. $uniq = array_unique($new);
  16. foreach($uniq as $ k=>$ser)
  17. $new1[$k] = unserialize($ser);
  18. return ($new1);
  19. }

  20. function a_array_unique($array)//written Better

  21. {
  22. $out = array();
  23. foreach ($array as $key=>$value) {
  24. if (!in_array($value, $out))
  25. {
  26. $out[$key] = $value;
  27. }
  28. }
  29. return $out;
  30. }
  31. ?>

Copy code

PHP’s own array_unique function only applies to one-dimensional arrays, not multi-dimensional arrays Applicable, here is a PHP two-dimensional array deduplication array_unique function.

Example:

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

Call example:

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


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!