(1) using the functions provided by PHP, Array_unique and ARRAY_DIFF_ASSOC to implement
Copy Code code as follows:
<?php
function Fetchrepeatmemberinarray ($array) {
Get an array that removes duplicate data
$unique _arr = Array_unique ($array);
Get an array of duplicate data
$repeat _arr = Array_diff_assoc ($array, $unique _arr);
return $repeat _arr;
}
Test Cases
$array = Array (
' Apple ',
' iphone ',
' Miui ',
' Apple ',
' Orange ',
' Orange '
);
$repeat _arr = Fetchrepeatmemberinarray ($array);
Print_r ($repeat _arr);
?>
(2) Write function to achieve this function, using two for loop
Copy Code code as follows:
<?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 Cases
$array = Array (
' Apple ',
' iphone ',
' Miui ',
' Apple ',
' Orange ',
' Orange '
);
$repeat _arr = Fetchrepeatmemberinarray ($array);
Print_r ($repeat _arr);
?>