Php implementation code for getting repeated data in an array

Source: Internet
Author: User
Php implementation code for getting repeated data in an array

  1. Function FetchRepeatMemberInArray ($ array ){
  2. // Obtain the array for removing duplicate data
  3. $ Unique_arr = array_unique ($ array );
  4. // Obtain the array of repeated 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. 'Miuis ',
  13. 'Apple ',
  14. 'Orange ',
  15. 'Orange'
  16. );
  17. $ Repeat_arr = FetchRepeatMemberInArray ($ array );
  18. Print_r ($ repeat_arr );
  19. ?>

Example 2: Use a UDF to implement this function.

  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. 'Miuis ',
  18. 'Apple ',
  19. 'Orange ',
  20. 'Orange'
  21. );
  22. $ Repeat_arr = FetchRepeatMemberInArray ($ array );
  23. Print_r ($ repeat_arr );
  24. ?>

>>> Articles you may be interested in: php removes duplicate data from arrays two examples php generates N non-repeated random numbers php prevents repeated forms submitted small example php gets repeated data in the array two methods one remove two-dimensional array repeated items in php function php judgment and removal of repeated data in the array method php prevent refresh page repeated submitted code example php prevent users refresh repeated submission method remove repeated combinations in two-dimensional array example of code php array_unique to remove duplicate values in one-dimensional array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.