How to delete duplicates in an array?

前端 未结 5 406
南旧
南旧 2020-12-19 07:36

How can I delete duplicates in array?

For example if I had the following array:

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

I want it

5条回答
  •  难免孤独
    2020-12-19 08:32

    You can filter them out using array_count_values():

    $array = array('1','1','2','3');
    $res = array_keys(array_filter(array_count_values($array), function($freq) {
        return $freq == 1;
    }));
    

    The function returns an array comprising the original values and their respective frequencies; you then pick only the single frequencies. The end result is obtained by retrieving the keys.

    Demo

提交回复
热议问题