PHP Gets the duplicate element instance tutorial in the array

Source: Internet
Author: User
Tags foreach

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]);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.