数组重复如何制作单个

时间:2013-05-19 13:55:36

标签: php arrays duplicates

我有例如var $ test,输出如下。你可以看到它的重复。 我怎么能让它不重复?它有什么功能吗?

Array ( 
       [title] => Array ( 
             [0] => this field is required 
             [1] => must be longer than2 
       ) 
       [subtitle] => Array ( 
             [0] => this field is required 
             [1] => must be longer than2 
       ) 
)    
Array ( 
       [title] => Array ( 
             [0] => this field is required 
             [1] => must be longer than2 
       ) 
       [subtitle] => Array ( 
             [0] => this field is required 
             [1] => must be longer than2 
       ) 
)    

预期结果:

Array ( 
       [title] => Array ( 
             [0] => this field is required 
             [1] => must be longer than2 
       ) 
       [subtitle] => Array ( 
             [0] => this field is required 
             [1] => must be longer than2 
       ) 
)    

2 个答案:

答案 0 :(得分:2)

function intersect($data=NULL){
      if(!empty($data)){$crashed = array();
      $crashed2 = array();
      foreach($data[0] as $key=>$val){ 
             if(!is_array($val)){
               $crashed[$key] = in_array($val,$data[1]);//return true if crashed(intersect)
              }else{
               $crashed2[$key] = intersect(array($val,$data[1]));
              }
      $crashed = array_merge($crashed,$crashed2);
       }
    }return $crashed;
   }
   $intersect =intersect(array($array1,$array2));
   print_r($intersect);

答案 1 :(得分:1)

你可以使用;

$unique = array_map("unserialize", array_unique(array_map("serialize", $array)));
print_r($unique);

See Live Demo