-
- function FetchRepeatMemberInArray($array) {
- // Get an array with duplicate data removed
- $unique_arr = array_unique ( $array );
- // Get an array with 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 );
-
- ?>
Copy code
Example 2, use two custom functions to implement this function times for loop.
-
- 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',
- 'miui',
- ' apple',
- 'orange',
- 'orange'
- );
- $repeat_arr = FetchRepeatMemberInArray ( $array );
- print_r ( $repeat_arr );
- ?>
Copy code
>>> You may feel Articles of interest:
Two examples of removing duplicate data from arrays in php
php generates N non-repeating random numbers
A small example of php preventing repeated submission of forms
Two ways to get duplicate data in an array using php
A php function to remove duplicate items from a two-dimensional array
How to determine and remove duplicate data in an array using php
An example of php code to prevent repeated submissions when refreshing the page
How to prevent users from refreshing and submitting repeatedly in php
Implementation code for removing repeated combinations in a two-dimensional array
php array_unique Example of removing duplicate values from a one-dimensional array
|