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:
array_count_values
go through array , create index each value , increment each time encounters again.array_filter
take created array , pass throughmorethanone
function defined earlier, if returnsfalse
, key/value pair removed.array_keys
discardvalue
portion of array creating array values being keys defined. final step gives result removes values existed more once within original array.
Comments
Post a Comment