0

I have for example var $test and the output is below. You can see its duplicated. How can i make it not duplicated? Is there any function for it?

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 
       ) 
)    

Expected result:

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 
       ) 
)    
Bas
  • 2,330
  • 4
  • 29
  • 68

2 Answers2

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);
Pank
  • 13,800
  • 10
  • 32
  • 45
Ayesha
  • 622
  • 2
  • 5
  • 17
1

You can use ;

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

See Live Demo

Baba
  • 94,024
  • 28
  • 166
  • 217
  • @ Baba sorry, wrong array, i have updated it. If i print_r then i get this array. The previous didnt work. – Bas May 19 '13 at 14:38
  • add your expected result to your question – Baba May 19 '13 at 14:39
  • Done Baba, just a singele, i hope you can help! – Bas May 19 '13 at 14:42
  • With that expected result .. the code i added would still work .... did you even try it first ? – Baba May 19 '13 at 14:45
  • Yes, he gives back 2 times Array ( [title] => Array ( [0] => this field is required [1] => must be longer than2 ) ) – Bas May 19 '13 at 14:50
  • Is that not what is in your expected result .. learn to help your self .. http://eval.in/30732 matches exactly what was in your expected result – Baba May 19 '13 at 14:56
  • In your array is an comma for the next, not in my example. And you have put 2 arrays i in one thats the differents i see. – Bas May 19 '13 at 15:00