如果我們是一維數據去重復項就不要說了有直接的函數了,但二維數據php未提供函數了,下面小編來給大家推薦兩個不錯的二維數組去重復項自定義函數吧。
例1
代碼如下 復制代碼
<?php
function unique_array_2d($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')
);
echo "<pre>";
print_r(unique_array_2d($array2D,true));
/**
* 將二維數組通過指定的 key 去重
*
* @param array $arr 要去重的數組
* @param array $by 指定key去重,該參數不指定將返回 array()
* @return array
*/
function array_multunique($arr,$by = array()) {
$temp = array();
foreach($arr as $key => $val) {
foreach($by as $v) {
$temp[$key] .= isset($val[$v]) ? $val[$v] : '';
}
}
return array_intersect_key($arr,array_unique($temp));
}
/*$aa = array (
array ('id' => 123, 'name' => '張三' ),
array ('id' => 123, 'name' => '李四' ),
array ('id' => 124, 'name' => '王五' ),
array ('id' => 125, 'name' => '趙六' ),
array ('id' => 126, 'name' => '趙六' )
);
$key = 'id';
array_multunique ($aa, array('id')); */
function array_remove_key($array,$keys){
if (!is_array($array) || !is_array($keys)){
return false;
}
foreach($array as $t){
foreach($keys as $k){
unset($t[$k]);
}
$doc[]=$t;
}
return $doc;
}
/*$array = array(
'0' => array('a' => 'aaaaa', 'b' => 'bbbbb', 'c' => array('d' => 'ddddd', 'e' => 'eeeee')),
'1' => array('a' => 'aaaaa', 'b' => 'bbbbb', 'c' => array('d' => 'ddddd', 'e' => 'eeeee'))
);
print_r( array_remove_key($array,array('c')));*/
function array_remove_key_val(&$a,$b,$c){
foreach ($a as $key=>$value){
if ( isset($value[$b]) && ($value[$b]==$c) ){
unset($a[$key]);
}
}
}
/*$a=array(
array('id'=>1,'num'=>10,'type'=>'news'),
array('id'=>2,'num'=>100,'type'=>'pic')
);
print_r( array_remove_key_val($a,"id","1") );*/
例2
代碼如下 復制代碼
/二維數組去掉重復值
function array_unique_fb($array2D){
foreach ($array2D as $v){
$v = join(",",$v); //降維,也可以用implode,將一維數組轉換為用逗號連接的字符串
$temp[] = $v;
}
$temp = array_unique($temp); //去掉重復的字符串,也就是重復的一維數組
foreach ($temp as $k => $v){
$temp[$k] = explode(",",$v); //再將拆開的數組重新組裝
}
return $temp;
}
//二維數組去掉重復值 並保留鍵值
function array_unique_fb($array2D){
foreach ($array2D as $k=>$v){
$v = join(",",$v); //降維,也可以用implode,將一維數組轉換為用逗號連接的字符串
$temp[$k] = $v;
}
$temp = array_unique($temp); //去掉重復的字符串,也就是重復的一維數組
foreach ($temp as $k => $v){
$array=explode(",",$v); //再將拆開的數組重新組裝
$temp2[$k]["id"] =$array[0];
$temp2[$k]["litpic"] =$array[1];
$temp2[$k]["title"] =$array[2];
$temp2[$k]["address"] =$array[3];
$temp2[$k]["starttime"] =$array[4];
$temp2[$k]["endtime"] =$array[5];
$temp2[$k]["classid"] =$array[6];
$temp2[$k]["ename"] =$array[7];
}
return $temp2;
}