function unique_array_2d($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')
);
echo " ";
print_r(unique_array_2d($array2D,true));
/**
* Deduplicate the two-dimensional array through the specified key
*
* @param array $arr Array to remove duplicates
* @param array $by Specify key to remove duplicates. If this parameter is not specified, array() will be returned
* @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") );*/
|