php - How to delete duplicates in an array? -


how can delete duplicates in array?

for example if had following array:

$array = array('1','1','2','3'); 

i want become

$array = array('2','3'); 

so want delete whole value if 2 of found

depending on php version, should work in versions of php >= 4.0.6 doesn't require anonymous functions require php >= 5.3:

function morethanone($val) {     return $val < 2; }   $a1 = array('1','1','2','3'); print_r(array_keys(array_filter(array_count_values($a1), 'morethanone'))); 

demo (change php version in drop-down select version of php using)

this works because:

  1. array_count_values go through array , create index each value , increment each time encounters again.
  2. array_filter take created array , pass through morethanone function defined earlier, if returns false, key/value pair removed.
  3. array_keys discard value portion of array creating array values being keys defined. final step gives result removes values existed more once within original array.

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -