php中array_unique去除二维数组中重复的数组
一个利用php中array_unique实现去除二级数组重复值的方法,有需要的朋友可参考参考.
实例代码如下:
- <?php
- $input = array(“a” => “green”,”", “red”,”b” => “green”, “”,”blue”, “red”,”c” => “witer”,”hello”,”witer”);
-
- $result = a_array_unique($input);??
- foreach($result as $aa)
- {
- echo $aa.”<br />”;
- }
- 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;
- }
- ?>
array_unique函数只适用于一维数组,对多维数组并不适用,但是我们利用遍历数据然后再进行操作就可以实现array_unique去除重复数组了.
实例代码如下:
- function unique_arr($array2D,$stkeep=false,$ndformat=true)
- {
-
- if($stkeep) $stArr = array_keys($array2D);
-
- if($ndformat) $ndArr = array_keys(end($array2D));
-
- 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));