remove duplicate from string in PHP

柔情痞子 提交于 2019-11-26 20:08:31

The shortest code would be:

$str = implode(',',array_unique(explode(',', $str)));

If it is the fastest... I don't know, it is probably faster then looping explicitly.

Reference: implode, array_unique, explode

Dealing with: $string = 'one,two,one,five,seven,bag,tea';

If you are generating the string at any point "up script", then you should be eliminating duplicates as they occur.

Let's say you are using concatenation to generate your string like:

$string='';
foreach($data as $value){
    $string.=(strlen($string)?',':'').some_func($value);
}

...then you would need to extract unique values from $string based on the delimiter (comma), then re-implode with the delimiter.


I suggest that you design a more direct method and deny duplicates inside of the initial foreach loop, like this:

foreach($data as $value){
    $return_value=some_func($value);  // cache the returned value so you don't call the function twice
    $array[$return_value]=$return_value;  // store the return value in a temporary array using the function's return value as both the key and value in the array.
}
$string=implode(',',$array);  // clean: no duplicates, no trailing commas

This works because duplicate values are never permitted to exist. All subsequent occurrences will be used to overwrite the earlier occurrence. This function-less filter works because arrays may not have two identical keys in the same array(level).

Alternatively, you can avoid "overwriting" array data in the loop, by calling if(!isset($array[$return_value])){$array[$return_value]=$return_value;} but the difference means calling the isset() function on every iteration. The advantage of using these associative key assignments is that the process avoids using in_array() which is slower than isset().

All that said, if you are extracting a column of data from a 2-dimensional array like:

$string='';
foreach($data as $value){
    $string.=(strlen($string)?',':'').$value['word'];
}

Then you could leverage the magic of array_column() without a loop like this:

echo implode(',',array_column($str,'word','word'));

And finally, for those interested in micro-optimization, I'll note that the single call of array_unique() is actually slower than a few two-function methods. Read here for more details.

The bottomline is, there are many ways to perform this task. explode->unique->implode may be the most concise method in some cases if you aren't generating the delimited string, but it is not likely to be the most direct or fastest method. Choose for yourself what is best for your task.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!