remove duplicates from comma separated string

前端 未结 5 1746
有刺的猬
有刺的猬 2021-01-13 07:06

Is there a better (faster) solution to remove duplicates from a comma separated string?

public function d($dep) { 
    if (strpos($dep,\',\') !== false) {
           


        
5条回答
  •  不知归路
    2021-01-13 07:46

    Many many thanks guys. Finally, we have two solutions (Pascal and Ericso gave the same solution) here and the question became which one is faster. To be honest, by faster, I meant what executes faster and if I take words of Gumbo, I get the combined functions of array_keys and array_flip is faster than array_unique. Because, the two other functions, implode and explode are same.

    return implode(',', array_unique(explode(',', $dep)));
    return implode(',', array_keys(array_flip(explode(',', $dep))));
    

    Finally I gave 1 up to all three ad many thanks that can't be measured / seen. I agree that either of the three solutions can be expected, but then took the answer of Gumbo believing that array_keys(array_flip()) is faster to execute than array_unique() in the given context.

    Cheers.

提交回复
热议问题