Php retrieves repeated data from the array. 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, two days ago, array_unique and a needed to find the duplicate data in the php array and summarize the two methods. here we will share them with you for your attention.
(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 );
?>
Using (1) functions provided by php, array_unique and...