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 ...