PHP Gets the implementation code for repeating data in the array

Source: Internet
Author: User
    1. function Fetchrepeatmemberinarray ($array) {
    2. Get an array that removes duplicate data
    3. $unique _arr = Array_unique ($array);
    4. Get an array of duplicate data
    5. $repeat _arr = Array_diff_assoc ($array, $unique _arr);
    6. return $repeat _arr;
    7. }
    8. Test Cases
    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, the custom function implements this function, taking advantage of the two-time 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 Cases
    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 be interested in the article: PHP Two example of removing duplicate data from an array PHP generates n non-repeating random numbers PHP to prevent the form from repeating the small example PHP Two ways to get duplicate data in an array a PHP function that removes duplicates from a two-dimensional array php to determine and remove duplicate data in the array PHP prevents the page from refreshing the repeated submission of the code a case of PHP prevents users from refreshing duplicate commits the implementation code of the repeating combination in the two-dimensional array is removed PHP Array_ Example of a unique one-dimensional array for removing duplicate values

  • 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.