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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -