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 and summarized the two methods. I would like to share them with you here. Please Note: (1) use the functions provided by php, array_unique and array_diff_assoc to implement [php] & lt ;? PhpfunctionFetchRepeatMemberInArray ($ arr...

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
[Php]
Function FetchRepeatMemberInArray ($ array ){
// Obtain the array for removing duplicate data
$ Unique_arr = array_unique ($ array );
// Obtain the array of repeated data
$ Repeat_arr = array_diff_assoc ($ array, $ unique_arr );
Return $ repeat_arr;
}
 
// Test case
$ Array = array (
'Apple ',
'IPhone ',
'Miuis ',
'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
[Php]
Function 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 ',
'Miuis ',
'Apple ',
'Orange ',
'Orange'
);
$ Repeat_arr = FetchRepeatMemberInArray ($ array );
Print_r ($ repeat_arr );
 
?>

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.