Home > Backend Development > PHP Tutorial > PHP implementation code for obtaining repeated data in an array

PHP implementation code for obtaining repeated data in an array

WBOY
Release: 2016-07-25 08:55:25
Original
996 people have browsed it
  1. function FetchRepeatMemberInArray($array) {
  2. // Get an array with duplicate data removed
  3. $unique_arr = array_unique ( $array );
  4. // Get an array with duplicate data
  5. $repeat_arr = array_diff_assoc ( $array, $unique_arr );
  6. return $repeat_arr;
  7. }
  8. // Test case
  9. $array = array (
  10. 'apple',
  11. 'iphone',
  12. 'miui',
  13. 'apple',
  14. 'orange' ,
  15. 'orange'
  16. );
  17. $repeat_arr = FetchRepeatMemberInArray ( $array );
  18. print_r ( $repeat_arr );
  19. ?>
Copy code

Example 2, use two custom functions to implement this function times for loop.

  1. function FetchRepeatMemberInArray($array) {
  2. $len = count ( $array );
  3. for($i = 0; $i < $len; $i ++) {
  4. for($j = $i + 1; $j < $len; $j ++) {
  5. if ($array [$i] == $array [$j]) {
  6. $repeat_arr [] = $array [$i];
  7. break;
  8. }
  9. }
  10. }
  11. return $repeat_arr;
  12. }
  13. // Test case
  14. $array = array (
  15. 'apple',
  16. 'iphone',
  17. 'miui',
  18. ' apple',
  19. 'orange',
  20. 'orange'
  21. );
  22. $repeat_arr = FetchRepeatMemberInArray ( $array );
  23. print_r ( $repeat_arr );
  24. ?>
Copy code

>>> You may feel Articles of interest: Two examples of removing duplicate data from arrays in php php generates N non-repeating random numbers A small example of php preventing repeated submission of forms Two ways to get duplicate data in an array using php A php function to remove duplicate items from a two-dimensional array How to determine and remove duplicate data in an array using php An example of php code to prevent repeated submissions when refreshing the page How to prevent users from refreshing and submitting repeatedly in php Implementation code for removing repeated combinations in a two-dimensional array php array_unique Example of removing duplicate values ​​from a one-dimensional array



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