Php implementation code for getting repeated data in an array
- 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 );
- ?>
Example 2: Use a UDF to implement this function.
- 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 );
- ?>
>>> Articles you may be interested in: php removes duplicate data from arrays two examples php generates N non-repeated random numbers php prevents repeated forms submitted small example php gets repeated data in the array two methods one remove two-dimensional array repeated items in php function php judgment and removal of repeated data in the array method php prevent refresh page repeated submitted code example php prevent users refresh repeated submission method remove repeated combinations in two-dimensional array example of code php array_unique to remove duplicate values in one-dimensional array |