PHP-PHP source code

Source: Internet
Author: User
This article lists three methods of removing repeated element functions and instance code from an array. The first two methods are to use loops to determine whether to delete repeated array values. The last one is more classic than the previous one. The array_flip () to change the value to the key name, and then return the duplicate key name will be lost. This article lists three methods of removing repeated element functions and instance code from an array. The first two methods are to use loops to determine whether to delete repeated array values. The last one is more classic than the previous one. The array_flip () to change the value to the key name, and then return the duplicate key name will be lost.

Script ec (2); script

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
{
Delmember ($ array, $ j); // delete it
$ J --; // re-check whether the supplemented elements are repeated.
}
$ J ++;
}
$ I ++;
}
}

// Delete the function of repeating elements in the array

The Code is as follows:
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]);
}

// Example:

The Code is as follows:
$ Output = array (1, 2, 2, 'www .111cn.net ', 5, 4, 4, 4, 2, 7, 5, 9, 10 );
Delsame ($ output );
While (list ($ key, $ value) = each ($ output ))
{
Echo "$ key: $ value "."
";
}


// Method 2

The Code is as follows:

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 only into 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 3

The Code is as follows:

$ Hills = array ("first" => "data1", "second" => "www.111cn.net", "third" => "data1 ");
$ Hills = array_flip ($ hills); // restore key name
$ Hills1 = array_flip ($ hills); // Delete duplicate
Print_r ($ hills1 );

/*
Array array_flip (array trans)
Array_flip () returns an inverted array. For example, the key name in trans is changed to a value, and the value in trans is changed to a key name.
Note that the value in trans must be valid key names, for example, integer or string. If the value type is incorrect, a warning will be issued, and the problematic key/value pair will not be reversed.
If the same value appears multiple times, the last key name is used as its value, and all others are lost.
Array_flip () returns false if it fails.

*/

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.