In php, array_unique removes repeated arrays from two-dimensional arrays. _ PHP Tutorial

Source: Internet
Author: User
In php, array_unique removes repeated arrays from two-dimensional arrays. In php, array_unique is used to remove duplicate values of list-level arrays. For more information, see. The code is as follows: Copy the code? Php $ inputarray (agreen, red, bgre): array_unique in php is used to remove duplicate values of list-level arrays. For more information, see.
The code is as follows:

$ 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 leaves a single element
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;
}
?>

The array_unique function is only applicable to one-dimensional arrays and does not apply to multi-dimensional arrays. However, we can use the traversal data and then perform operations to remove repeated arrays.

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;
}
$ Array2D = array ('first' => array ('title' => '000000', 'date' => '000000 ′), 'second' => array ('title' => '123', 'date' => '123 ′), 'third' => array ('title' => '000000', 'date' => '000000 ′));
Print_r ($ array2D );
Print_r (unique_arr ($ array2D, true ));

Bytes. The code is as follows? Php $ input = array (a = green, red, B = gre...

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.