PHP retrieves repeated data from the array

Source: Internet
Author: User
Two days ago, I needed to find the duplicate data in the PHP array. I summarized the two methods and shared them with you here. Please pay attention to them.

(1) Using functions provided by PHP, array_unique and array_diff_assoc

 
<? Phpfunction fetchrepeatmemberinarray ($ array) {// get the array to remove duplicate data $ unique_arr = array_unique ($ array); // get the array of duplicate data $ repeat_arr = array_diff_assoc ($ array, $ unique_arr); return $ repeat_arr;} // test case $ array = array ('apple', 'iphone ', 'miui', 'apple', 'Orange ', 'Orange '); $ repeat_arr = fetchrepeatmemberinarray ($ array); print_r ($ repeat_arr);?>

(2) write the function by yourself to implement this function, using two for Loops

<? Phpfunction fetchrepeatmemberinarray ($ array) {$ Len = count ($ array); For ($ I = 0; $ I <$ Len; $ I ++) {for ($ J = $ I + 1; $ j <$ Len; $ J ++) {if ($ array [$ I] = $ array [$ J]) {$ repeat_arr [] = $ array [$ I]; break ;}}return $ repeat_arr;} // test case $ array = array ('apple', 'iphone ', 'miui ', 'apple', 'Orange', 'Orange '); $ repeat_arr = fetchrepeatmemberinarray ($ array); print_r ($ repeat_arr);?>

If you have a better way, You can paste it together and learn it together.


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.