array_unique(array) 只能处理value只有单个的数组。
去除有多个value数组,可以使用如下函数实现:
function more_array_unique($arr=array()){ foreach($arr[0] as $k => $v){ $arr_inner_key[]= $k; //先把二维数组中的内层数组的键值记录在在一维数组中 } foreach ($arr as $k => $v){ $v =join(",",$v); //降维 用implode()也行 $temp[$k] =$v; //保留原来的键值 $temp[]即为不保留原来键值 } $temp =array_unique($temp); //去重:去掉重复的字符串 foreach ($temp as $k => $v){ $a = explode(",",$v); //拆分后的重组 如:Array( [0] => james [1] => 30 ) $arr_after[$k]= array_combine($arr_inner_key,$a); //将原来的键与值重新合并 } return $arr_after; }
有如下数组:
$arr = array( '0'=>array( 'name'=>'james', 'age'=>30, ), '1'=>array( 'name'=>'susu', 'age'=>26, ), '2'=>array( 'name'=>'james', 'age'=>30, ), 'new'=>array( 'name'=>'kube', 'age'=>37, ), 'list'=>array( 'name'=>'kube', 'age'=>27, ), '435'=>array( 'name'=>'susu', 'age'=>26, ), '33'=>array( 'name'=>'james', 'age'=>30, ), );
调用函数后显示:
Array
(
[0] => Array
(
[name] => james
[age] => 30
)
[1] => Array
(
[name] => susu
[age] => 26
)
[new] => Array
(
[name] => kube
[age] => 37
)
[list] => Array
(
[name] => kube
[age] => 27
)
)
测试Demo如下:
<?php $arr = array( '0'=>array( 'name'=>'james', 'age'=>30, ), '1'=>array( 'name'=>'susu', 'age'=>26, ), '2'=>array( 'name'=>'james', 'age'=>30, ), 'new'=>array( 'name'=>'kube', 'age'=>37, ), 'list'=>array( 'name'=>'kube', 'age'=>27, ), '435'=>array( 'name'=>'susu', 'age'=>26, ), '33'=>array( 'name'=>'james', 'age'=>30, ), ); printf("原来的数组:<br><pre>"); //输出原来的数组 print_r($arr); echo "</pre><br/>"; function more_array_unique($arr=array()){ foreach($arr[0] as $k => $v){ $arr_inner_key[]= $k; //先把二维数组中的内层数组的键值记录在在一维数组中 } foreach ($arr as $k => $v){ $v =join(",",$v); //降维 用implode()也行 $temp[$k] =$v; //保留原来的键值 $temp[]即为不保留原来键值 } $temp =array_unique($temp); //去重:去掉重复的字符串 foreach ($temp as $k => $v){ $a = explode(",",$v); //拆分后的重组 如:Array( [0] => james [1] => 30 ) $arr_after[$k]= array_combine($arr_inner_key,$a); //将原来的键与值重新合并 } return $arr_after; } $arr_new = more_array_unique($arr); //调用去重函数 printf("去除重复元素后的数组:<br>"); echo '<pre>'; print_r($arr_new); echo "</pre><br/>"; ?>