Home >Backend Development >PHP Tutorial >A small example of php array deduplication

A small example of php array deduplication

WBOY
WBOYOriginal
2016-07-25 09:05:40840browse
  1. function assoc_unique($arr, $key) {
  2. $tmp_arr = array();
  3. foreach($arr as $k => $v) {
  4. if(in_array($v [$key], $tmp_arr)) {
  5. unset($arr[$k]);
  6. } else {
  7. $tmp_arr[] = $v[$key];
  8. }
  9. }
  10. sort($arr);
  11. return $arr;
  12. }
  13. $aa = array(
  14. array('id' => 123, 'name' => '张三'),
  15. array('id' => 123, 'name' => '李思'),
  16. array('id' => 124, 'name' => '王五'),
  17. array('id' => 125, 'name' => ' Zhao Liu'),
  18. array('id' => 126, 'name' => 'Zhao Liu')
  19. );
  20. $key = 'name';
  21. assoc_unique(&$aa, $key);
  22. print_r($aa);
  23. ?>
Copy code

Articles you may be interested in: Introduction to how to create an array in php php uses array_unique to determine whether the same value exists in the array Small examples of php arrays and loops php method to find a specific value in a multidimensional array Enhanced version of array_unique function (supports two-dimensional array) Learn to sort multi-dimensional arrays in php php multidimensional array sorting example Application example of php array callback filter function array_filter() PHP functions and application examples for extracting variables from arrays PHP code for sorting multi-dimensional arrays by a certain value php code to randomly extract some elements from an array



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