2

I have this string: $entitlemnet = '43857403,erot,43857403,erot,rejh'

I want to remove all the duplicate values from this string. How can i do that?

See code below.

$roql_Ent_result = RNCPHP\ROQL::query($EntQuery)->next();

while ($Ent_result = $roql_Ent_result->next())
{
    $Entitlement = $Ent_result['Entitlement'];
    $findme = $Entitlement.",";
    $pos = stripos($EntitlementString, $findme);

    if ($pos === false)
    {
        $EntitlementString = $EntitlementString.$Entitlement.", "; 
    }
}

if ($EntitlementString != "")
{
    $EntitlementString = substr($EntitlementString,0,(strlen($EntitlementString)-2)).";";
}
1
  • do you mean this string 43857403,erot,43857403,erot,rejh will result to 43857403,erot,rejh ? Commented Jan 14, 2016 at 8:45

3 Answers 3

2

If you simply want to remove the duplicates from the string.

$entitlemnet = '43857403,erot,43857403,erot,rejh';
$unique_string = implode(',', array_unique(explode(',', $entitlemnet)));
  1. Turn your string into an array by splitting it with explode() by delimiter ','

  2. Remove duplicate values with the function array_unique()

  3. Turn the array back into a string by concatenating the array values with delimiter ','

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

$entitlemnet = "43857403,erot,43857403,erot,rejh";
$result = implode(',', array_unique(explode(',', $entitlemnet)));
echo $result;

Comments

0

I think you can do it like this:

$entitlemnet = "43857403,erot,43857403,erot,rejh";
$result = implode(',', array_unique(explode(',', $entitlemnet)));
echo $result;

Will result in:

43857403,erot,rejh

2 Comments

Thank you so much, this code is working fine, But if their is a space in the starting like 'STOR-APD, 001-0669-001,ISSP3.0,CXFS7,STOR-SAN,STOR-CXFS, STOR-DMF, STOR-TMF, 001-0586-030, DMF6,STOR-DMF,001-0669-001, XFS-XVM6, XFS-XVM-RHEL6,STOR-XVM, STOR-NAS, 001-9007-001, STOR-LUSTRE' its not deleting duplicates.
@hemanthkumar You can use array_map and trim. $result = implode(',', array_unique(array_map('trim', explode(',', $entitlemnet))));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.