Home > Backend Development > PHP Tutorial > PHP array deduplication function code example

PHP array deduplication function code example

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

  2. * Function to remove duplicate values ​​from array
  3. * by bbs.it-home.org
  4. */
  5. function array_assoc_unique($arr, $key) {
  6. $tmp_arr = array();
  7. foreach($arr as $k => $v) {
  8. if(in_array($v[$key], $tmp_arr)) {
  9. unset($arr[$k]);
  10. } else {
  11. $tmp_arr[] = $v[$key ];
  12. }
  13. }
  14. sort($arr);
  15. return $arr;
  16. }

  17. //Call the example to remove duplicate values ​​in the data

  18. $aa = array(
  19. array('id ' => 123, 'name' => 'Scripting School'),
  20. array('id' => 123, 'name' => 'php programming'),
  21. array('id' => 124, 'name' => 'Programmer's Home'),
  22. array('id' => 125, 'name' => 'Self-taught'),
  23. array('id' => 126, 'name' => 'Programmer's Home')
  24. );
  25. $key = 'name';
  26. array_assoc_unique(&$aa, $key);
  27. print_r($aa);
  28. ?>
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