Php removes repeated data from the array _ php instance

Source: Internet
Author: User
Removing repeated data from an array is also called deduplication. The difference between the function provided in this article and the native php array_unique is that array_unique requires a string, and this function can be an array or an object, directly Add code

The Code is as follows:


/**
* Weight sorting for Arrays
* Difference from the array_unique function: it requires that val be a string, and this can be an array/object.
*
* @ Param unknown_type $ array to which arr is to be duplicated
* @ Param unknown_type $ reserveKey whether the original Key is retained
* @ Return unknown
*/
Static function m_ArrayUnique ($ arr, $ reserveKey = false)
{
If (is_array ($ arr )&&! Empty ($ arr ))
{
Foreach ($ arr as $ key => $ value)
{
$ TmpArr [$ key] = serialize ($ value ).'';
}
$ TmpArr = array_unique ($ tmpArr );
$ Arr = array ();
Foreach ($ tmpArr as $ key => $ value)
{
If ($ reserveKey)
{
$ Arr [$ key] = unserialize ($ value );
}
Else
{
$ Arr [] = unserialize ($ value );
}
}
}
Return $ arr;
}


The code is very simple, but it is very practical. You can use it directly if you need it.

Other solutions provided by other netizens

The Code is as follows:


<? Php
$ Input = array ("a" => "green", "", "red", "B" => "green", "", "blue ", "red", "c" => "witer", "hello", "witer ");
// $ Result = array_unique ($ input); // remove duplicate elements
$ Result = a_array_unique ($ input); // only a single element is left.
Foreach ($ result as $ aa)
{
Echo $ aa ."
";
}
Function multi_unique ($ array ){
Foreach ($ array as $ k => $ na)
$ New [$ k] = serialize ($ na );
$ Uniq = array_unique ($ new );
Foreach ($ uniq as $ k => $ ser)
$ New1 [$ k] = unserialize ($ ser );
Return ($ new1 );
}

Function a_array_unique ($ array) // write better
{
$ Out = array ();
Foreach ($ array as $ key => $ value ){
If (! In_array ($ value, $ out ))
{
$ Out [$ key] = $ value;
}
}
Return $ out;
}
?>

PHP array deduplication has a built-in function array_unique (), but the php array_unique function is only applicable to one-dimensional arrays and not to multidimensional arrays. The following provides a two-dimensional array array_unique Function

The Code is as follows:


Function unique_arr ($ array2D, $ stkeep = false, $ ndformat = true)
{
// Determine whether the primary array key is retained (the primary array key can be non-numeric)
If ($ stkeep) $ stArr = array_keys ($ array2D );
// Determine whether the secondary array key is retained (all secondary array keys must be the same)
If ($ ndformat) $ ndArr = array_keys (end ($ array2D ));
// Dimensionality reduction. You can also use implode to convert a one-dimensional array to a string connected with commas (,).
Foreach ($ array2D as $ v ){
$ V = join (",", $ v );
$ Temp [] = $ v;
}
// Remove the duplicate string, that is, the duplicate one-dimensional array.
$ Temp = array_unique ($ temp );
// Re-assemble the split Array
Foreach ($ temp as $ k => $ v)
{
If ($ stkeep) $ k = $ stArr [$ k];
If ($ ndformat)
{
$ TempArr = explode (",", $ v );
Foreach ($ tempArr as $ ndkey => $ ndval) $ output [$ k] [$ ndArr [$ ndkey] = $ ndval;
}
Else $ output [$ k] = explode (",", $ v );
}
Return $ output;
}

Demo:

$ Array2D = array ('first' => array ('title' => '123', 'date' => '123 '), 'second' => array ('title' => '123', 'date' => '123 '), 'third' => array ('title' => '123456', 'date' => '123456 '));
Print_r ($ array2D );
Print_r (unique_arr ($ array2D, true ));

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.