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