PHP Delete Array repeat element function

Source: Internet
Author: User

Function Delsame (& $array)
{
$i = 0;
while (Isset ($array [$i]))
{
$j = $i + 1;
while (Isset ($array [$j]))
{
if ($array [$i] = = $array [$j])//If duplicate elements are found behind
{
Delmember ($array, $j); To remove it
$j--; Re-check to see if the elements that were filled are duplicates
}
$j + +;
}
$i + +;
}
}

function to delete a repeating element in an array

The code is as follows Copy Code
Function Delmember (& $array, $id)
{
$size = count ($array);
for ($i = 0; $i < $size-$id-1; $i + +)
{
$array [$id + $i] = $array [$id + $i + 1];
}
Unset ($array [$size-1]);
}

Use examples:

The code is as follows Copy Code
$output = Array (1, 2, 2, ' www.111cn.net ', 5, 4, 4, 4, 2, 7, 5, 9, 10);
Delsame ($output);
while (the list ($key, $value) = each ($output))
{
echo "$key: $value". <br> ";
}


Method Two

The code is as follows Copy Code

function Uniquearray ($array)

{

Get unique ELTs as keys in Assoc. Array

For ($i =0, $n =count ($array, 1); $i < $n; $i)

$u _array[$array [$i]] = 1;

Copy keys to another array

Reset ($u _array, 1);

For ($i =0, $n =count ($u _array, 1); $i < $n; $i) {

$unduplicated _array[] = key ($u _array, 1);

Next ($u _array, 1);

}

return $unduplicated _array;

}

Method Three

The code is as follows Copy Code

$hills =array ("=>" "Data1", "second" => "" data1/">www.111cn.net", "third" => "data1");
$hills =array_flip ($hills); Restore Key Name
$hills 1=array_flip (Array_flip ($hills));//Delete duplicates
Print_r ($hills 1);

/*
Array Array_flip (array trans)
Array_flip () returns an inverted array, for example, the key name in trans becomes a value, and the value in trans is the key name.
Note that the value in trans needs to be able to be a valid key name, such as an integer or string. A warning is issued if the type of the value is not correct, and the problematic key/value pair will not be reversed.
If the same value occurs more than once, the last key name is the value, and all the others are lost.
Array_flip () If the failure returns false.

*/

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.