Remove duplicate value in string php

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.

Quick tips on how to remove duplicate values from a comma separate string in PHP

  • 18 Oct, 2020
  • 2 min read

For all examples in this article, let's suppose we have the following comma separated string, which we want to remove duplicate values from:

$str = 'foo,bar,foo,baz';

Using array_unique[]

We can remove duplicates from a string in the following three steps:

  1. Convert comma separated string to an array;
  2. Use array_unique[] to remove duplicates;
  3. Convert the array back to a comma separated string.
$uniqueStr = implode[',', array_unique[explode[',', $str]]];

echo $uniqueStr; // output: 'foo,bar,baz'

Both explode[] and implode[] in this case would have a runtime complexity of O[n]. array_unique[], however, would have the runtime complexity of O[n log n] since it first sorts the array and then does a linear scanning. Following the rules of Big O, we drop O[n] and keep the most dominant term O[n log n] as it has a much heavier impact on performance. This gives us an overall runtime complexity of O[n log n]. That being said, this method is more readable as it is immediately clear from reading the code what we're trying to do.

Using array_flip[] and array_keys[]

Since array keys are unique in PHP, if we flip the array key/value associations, duplicates would be removed automatically. We can achieve this in the following steps:

  1. Convert comma separated string to an array;
  2. Use array_flip[] to swap key/value association. In doing so duplicates are automatically discarded;
  3. Use array_keys[] to create an array with keys as values;
  4. Convert the array back to a comma separated string.
$uniqueStr = implode[',', array_keys[array_flip[explode[',', $str]]]];

echo $uniqueStr; // output: 'foo,bar,baz'

array_flip[], array_keys[], explode[] and implode[] all have a time complexity of O[n] in this case. Therefore, the overall runtime complexity of using this method would be O[n]. That being said, this method is not very readable as it may not immediately be clear from reading the code what we're trying to do.

Hope you found this post useful. It was published 18 Oct, 2020. Please show your love and support by sharing this post.

  • Web Development
  • Backend
  • PHP

How to remove duplicate string value in PHP?

Use the array_unique[] Function to Remove Duplicate Values From an Array in PHP. We can use the array_unique[] function to remove duplicate values from an array. The array_unique[] function is a specialized function for removing the duplicate values from an array.

How do you remove duplicates from a comma separated string?

We can remove duplicates from a string in the following three steps: Convert comma separated string to an array; Use array_unique[] to remove duplicates; Convert the array back to a comma separated string.

How do you print duplicate characters from a string in PHP?

php $string = "learnetutorials.com"; $string = strtolower[$string]; $size = strlen[$string]; echo "The entered string is : $string \n"; echo "The duplicate characters in the string are: \n"; for [$i = 0; $i < $size; $i++] { $count = 1; for [$j = $i + 1; $j < $size; $j++] { if [$string[$i] == $string[$j] && $string[$i] ...

How do I remove comma separated duplicate values in Excel?

Problem: You have the same words or text strings in a cell and would like to remove the second and all subsequent repeats..
Select a range of cells from which you want to remove repeated text..
Press Alt + F8 to open the Macro dialog box..
In the list of macros, select RemoveDupeWords2..
Click Run..

Chủ Đề