A simple example of php array deduplication (one-dimensional and two-dimensional array deduplication)

WBOY
Release: 2016-07-25 08:55:38
Original
1158 people have browsed it
  1. $aa=array("apple","banana","pear","apple","wail","watermalon");
  2. $bb=array_unique($aa);
  3. print_r($bb);
  4. ?>
Copy the code

Output result: Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon ) .

Second, duplicates of PHP two-dimensional array: For two-dimensional arrays, we will discuss two situations. One is because the value of a certain key name cannot be repeated and duplicates are deleted; The other is to remove duplicates because the internal one-dimensional arrays cannot be exactly the same.

Example 1, because the value of a certain key name cannot be repeated, delete the duplicates. Code:

  1. function assoc_unique($arr, $key)
  2. {
  3. $tmp_arr = array();
  4. foreach($arr as $k => $v)
  5. {
  6. if(in_array ($v[$key], $tmp_arr))//Search whether $v[$key] exists in the $tmp_arr array. If it exists, return true
  7. { // bbs.it-home.org
  8. unset($arr[ $k]);
  9. }
  10. else {
  11. $tmp_arr[] = $v[$key];
  12. }
  13. }
  14. sort($arr); //sort function sorts the array
  15. return $arr;
  16. }
  17. $aa = array(
  18. array('id' => 123, 'name' => 'Zhang San'),
  19. array('id' => 123, 'name' => '李思') ,
  20. array('id' => 124, 'name' => '王五'),
  21. array('id' => 125, 'name' => 'Zhao Liu'),
  22. array( 'id' => 126, 'name' => 'Zhao Liu')
  23. );
  24. $key = 'id';
  25. assoc_unique(&$aa, $key);
  26. print_r($aa);
  27. ? >
Copy code

Output result: Array ( [0] => Array ( [id] => 123 [name] => Zhang San) [1] => Array ( [id] => 124 [name] => Wang Wu) [2] => Array ( [id] => 125 [name] => Zhao Liu) [3] => Array ( [id] => 126 [name] => Zhao Liu ) ) Example 2: Delete duplicates because the internal one-dimensional arrays cannot be exactly the same. Code:

  1. function array_unique_fb($array2D){
  2. foreach ($array2D as $v){
  3. $v = join(",",$v); //Dimension reduction can also be used implode, convert a one-dimensional array into a string connected by commas
  4. $temp[] = $v;
  5. } // bbs.it-home.org
  6. $temp = array_unique($temp); //Remove repeated characters String, that is, a repeated one-dimensional array
  7. foreach ($temp as $k => $v){
  8. $temp[$k] = explode(",",$v); //Then split the array Reassemble
  9. }
  10. return $temp;
  11. }
  12. $aa = array(
  13. array('id' => 123, 'name' => 'Zhang San'),
  14. array('id' => 123 , 'name' => '李思'),
  15. array('id' => 124, 'name' => '王五'),
  16. array('id' => 123, 'name' => '李思'),
  17. array('id' => 126, 'name' => 'Zhao Liu')
  18. );
  19. $bb=array_unique_fb($aa);
  20. print_r($bb)
  21. ?>
Copy the code

Output result:

Array ( [0] => Array ( [0] => 123 [1] => Zhang San) [1] => Array ( [0] => 123 [1] => Li Si) [2] => Array ( [0] => 124 [1] => Wang Wu) [4] => Array ( [0] => 126 [1] => Zhao Liu ) )


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!