PHP Array_unique Removing duplicate arrays in a two-dimensional array _php tutorial

Source: Internet
Author: User
A method of using PHP Array_unique implementation of the elimination of two-level array duplicate values, the need for friends can refer to.
The code is as follows Copy Code

$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);?? Leaving only 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)
$new 1[$k] = unserialize ($ser);
Return ($new 1);
}

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 applies only to one-dimensional arrays and does not apply to multidimensional arrays, but we can implement array_unique to remove duplicate arrays by traversing the data and then manipulating it.

code as follows copy code

function Unique_arr ($array, $stkeep =false, $ndformat =true)
{
Determines whether to preserve the first-level array key (the first-level array key can be non-numeric)
if ($stkeep) $STARR = Array_keys ($array 2D);
Determines whether to preserve level two array keys (all two-level array keys must be the same)
if ($ndformat) $NDARR = Array_keys (end ($array 2D));
Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
foreach ($array 2D as $v) {
$v = Join (",", $v);
$temp [] = $v;
}
Remove duplicate strings, that is, duplicate one-dimensional arrays
$temp = Array_unique ($temp);
Re-assemble the disassembled 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;
}
$array 2D = Array (' First ' =>array (' title ' = ' 1111′ ', ' date ' = ' 2222′ '), ' second ' =>array (' title ' = ' 1111′ ', ' Date ' = ' 2222′), ' third ' =>array (' title ' = ' 2222′ ', ' date ' = ' 3333′ ');
Print_r ($array 2D);
Print_r (Unique_arr ($array 2d,true));

http://www.bkjia.com/PHPjc/628911.html www.bkjia.com true http://www.bkjia.com/PHPjc/628911.html techarticle a method of using PHP Array_unique implementation of the elimination of two-level array duplicate values, the need for friends can refer to. Code to copy code 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.