0

我有例如 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 
       ) 
)    
4

2 回答 2

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);
于 2013-05-19T16:12:15.863 回答
1

您可以使用 ;

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

观看现场演示

于 2013-05-19T14:23:06.040 回答