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
<? Phpfunction fetchrepeatmemberinarray ($ array) {// get the array to remove duplicate data $ unique_arr = array_unique ($ array); // get the array of duplicate data $ repeat_arr = array_diff_assoc ($ array, $ unique_arr); return $ repeat_arr;} // test case $ array = array ('apple', 'iphone ', 'miui', '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
<? Phpfunction 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 ', 'miui ', 'apple', 'Orange', 'Orange '); $ repeat_arr = fetchrepeatmemberinarray ($ array); print_r ($ repeat_arr);?>
If you have a better way, You can paste it together and learn it together.