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 );
?>