Jump to content

find and delete recurring numbers


JKG

Recommended Posts

hello, is there any way that i can take a string like this:

 

99,99,28,99,28,112,78,99,28,112,78,28,112,78,112,78,112,

 

and find and remove the recurring numbers so i am left with this:?

 

99,28,112,78

 

thanks for your time

Joe :)

Link to comment
Share on other sites

$string = '99,99,28,99,28,112,78,99,28,112,78,28,112,78,112,78,112,';
$string = trim(implode(',', array_unique(explode(',', $string))), ',');

 

Explanation:

 

explode() creates an array with each part separated by a comma as a value (note; there will be an empty value at the end due to the trailing space)

array_unique() removes duplicate values from the array

implode() Converts the array (of only unique values) back into a string separated by commas

trim() removes the trailing comma due to the empty values in the original array

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.