A method to use array_unique in PHP to remove duplicate values from secondary arrays. Friends in need can refer to it.
The code is as follows
代码如下 |
复制代码 |
$input = array(“a” => “green”,”", “red”,”b” => “green”, “”,”blue”, “red”,”c” => “witer”,”hello”,”witer”);
//$result = array_unique($input); //去除重复元素
$result = a_array_unique($input);?? //只留下单一元素
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)//写的比较好
{
$out = array();
foreach ($array as $key=>$value) {
if (!in_array($value, $out))
{
$out[$key] = $value;
}
}
return $out;
}
?>
|
|
Copy code
|
代码如下 |
复制代码 |
function unique_arr($array2D,$stkeep=false,$ndformat=true)
{
// 判断是否保留一级数组键 (一级数组键可以为非数字)
if($stkeep) $stArr = array_keys($array2D);
// 判断是否保留二级数组键 (所有二级数组键必须相同)
if($ndformat) $ndArr = array_keys(end($array2D));
//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
foreach ($array2D as $v){
$v = join(“,”,$v);
$temp[] = $v;
}
//去掉重复的字符串,也就是重复的一维数组
$temp = array_unique($temp);
//再将拆开的数组重新组装
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’=>’1111′,’date’=>’2222′),’second’=>array(‘title’=>’1111′,’date’=>’2222′),’third’=>array(‘title’=>’2222′,’date’=>’3333′));
print_r($array2D);
print_r(unique_arr($array2D,true));
|
$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 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)//Written better
{
$out = array();
foreach ($array as $key=>$value) {
if (!in_array($value, $out))
{
$out[$key] = $value;
}
}
return $out;
}
?>
The array_unique function only applies to one-dimensional arrays and is not applicable to multi-dimensional arrays. However, we can achieve array_unique to remove duplicate arrays by traversing the data and then performing operations.
The code is as follows
|
Copy code
function unique_arr($array2D,$stkeep=false,$ndformat=true)
{
// Determine whether to retain the first-level array key (the first-level array key can be non-numeric)
if($stkeep) $stArr = array_keys($array2D);
// Determine whether to retain the secondary array keys (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 into a string connected with commas
foreach ($array2D as $v){
$v = join(“,”,$v);
$temp[] = $v;
}
//Remove repeated strings, that is, repeated one-dimensional arrays
$temp = array_unique($temp);
//Reassemble 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;
}
$array2D = array('first'=>array('title'=>'1111′,'date'=>'2222′),'second'=>array('title'=>'1111 ′,'date'=>'2222′),'third'=>array('title'=>'2222′,'date'=>'3333′));
print_r($array2D);
print_r(unique_arr($array2D,true));
http://www.bkjia.com/PHPjc/628911.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628911.htmlTechArticleA method to use array_unique in PHP to remove duplicate values from secondary arrays. Friends in need can refer to it. The code is as follows Copy the code ?php $input = array(a = green,, red,b = gre...
|
|