Home >Backend Development >PHP Tutorial >A php function to remove duplicates from a two-dimensional array

A php function to remove duplicates from a two-dimensional array

WBOY
WBOYOriginal
2016-07-25 08:58:571073browse
  1. //Function 1, two-dimensional array to remove duplicate values ​​

  2. function array_unique_fb($array2D){
  3. foreach ($array2D as $v){
  4. $v = join( ",",$v); //For dimensionality reduction, you can also use implode to convert a one-dimensional array into a string connected with commas
  5. $temp[] = $v;
  6. }
  7. $temp = array_unique($temp) ; //Remove repeated strings, that is, repeated one-dimensional arrays
  8. foreach ($temp as $k => $v){
  9. $temp[$k] = explode(",",$v); / /Reassemble the disassembled array
  10. }
  11. return $temp;
  12. }

  13. //Function 2, two-dimensional array removes duplicate values ​​and retains key values

  14. function array_unique_fb($array2D) {
  15. foreach ($array2D as $k=>$v){
  16. $v = join(",",$v); //For dimensionality reduction, you can also use implode to convert one-dimensional arrays into comma-connected ones String
  17. $temp[$k] = $v;
  18. }
  19. $temp = array_unique($temp); //Remove repeated strings, that is, repeated one-dimensional arrays
  20. foreach ($temp as $k => ; $v){
  21. $array=explode(",",$v); //Reassemble the disassembled array
  22. $temp2[$k]["id"] =$array[0];
  23. $ temp2[$k]["litpic"] =$array[1];
  24. $temp2[$k]["title"] =$array[2];
  25. $temp2[$k]["address"] =$ array[3];
  26. $temp2[$k]["starttime"] =$array[4];
  27. $temp2[$k]["endtime"] =$array[5];
  28. $temp2[$k] ["classid"] =$array[6];
  29. $temp2[$k]["ename"] =$array[7];
  30. }
  31. return $temp2;
  32. }
  33. ?>

Copy code

Call example:

  1. $arr = array("a"=>array("a"=>"welcome","b"=>"to","c"=> "bbs.it-home.org"),
  2. "b"=>array("a"=>"Programmer's Home","b"=>"jbxue.com","c"=> ;"beijing")
  3. );
  4. $arr2 = array_unique_fb($arr)
  5. ?>
Copy code

Articles you may be interested in: Two examples of removing duplicate data from arrays in php Example analysis of PHP two-dimensional array deduplication PHP array deduplication function code example A simple example of php array deduplication (one-dimensional and two-dimensional array deduplication) PHP array deduplication method reference (one-dimensional array deduplication, two-dimensional array deduplication) How to determine and remove duplicate data in an array using php Implementation code for removing repeated combinations in a two-dimensional array php custom function to remove duplicates from two-dimensional array php array_unique example of removing duplicate values ​​from a one-dimensional array Small example of php array deduplication



Statement:
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