1. Gets the duplicated elements in an array. The code is as follows:
A. Method I:
| The code is as follows |
|
|
function Array_repeat ($arr)
{
if (!is_array ($arr)) return $arr;
$arr 1 = array_count_values ($arr);
$NEWARR = Array ();
foreach ($arr 1 as $k => $v)
{
if ($v >1) Array_push ($NEWARR, $k);
}
return $NEWARR;
} |
B. Method II:
| The code is as follows |
|
|
function Array_repeat ($arr)
{
if (!is_array ($arr)) return $arr;
$arr 1 = array_unique ($arr);
$arr 3 = Array_diff_key ($arr, $arr 1);
Return Array_unique ($arr 3);
} |
Test method
| The code is as follows |
|
|
$arr = Array (' Apple ', ' blue ', ' red ', ' banana ', ' pear ', ' apple ', ' blue ', ' color ', ' color ');
Print_r (Array_repeat ($arr));
Result: Array ([5] => Apple [6] => Blue [8] => color) |
Later on, some ways to delete an array of repeating elements
Array_keys function to delete an array repeating element.
*/
| The code is as follows |
|
|
$a =array ("Red", "green", "blue", "yellow");
Count ($a); Get 4
unset ($a [1]); Delete a second element
Count ($a); Get 3
echo $a [2]; There are only three elements in the array, and you want to get the last element, but get blue,
echo $a [1]; No value
Array array_splice (array input, int offset [, int length [, array replacement]])
Array_splice () is actually a function that replaces an array element, but simply deletes the element without replacing it. The following is the use of Array_splice ():
$b =array ("Red", "green", "blue", "yellow");
Array_splice ($a, 1, 1); |
Let's look at a more comprehensive deletion of duplicate values and delete the specified array elements
| code as follows |
|
|
$array 1 = array (1 => "Www.45it.net", 2 => "Pineapple", 4 => "Www.45it.net", 3 => "Banana", 4 => "ba le", 5 => "Www.45it.net", 6 => "www.45it.net");
$search _keys = Array_keys ($array 1, "www.45it.net");
foreach ($search _keys as $key) {
unset ($array 1[$key]);
}
Print_r ($array 1);
/*
Get Results
Array ([2] => pineapple [4] => [3] => banana)
*/
//Remove duplicate elements in array function
functions Delmember (&A MP $array, $id)
{
$size = count ($array),
for ($i = 0; $i < $size-$id-1; $i + +)
{
$array [$id + $i] = $array [$id + $i + 1];
}
unset ($array [$size-1]);
} |