Delete repeated values from a variable

Asked

Viewed 1,214 times

3

I’ll just give you a simpler example so I don’t have to post the whole code, I don’t know if that’s possible.

$exemplo = "exemplo exemplo"

echo $exemplo;

When displaying this variable with echo it returns "example example" it would have some command or something q can use for it to return only "example" without repeating the word?

Note: I can not change the value of the variable nor by , because the way I use to capture it returns me exactly like this above.

  • Are the repeated values always separated by "space"? In this case this variable is a normal string right? You could turn this variable into an array, and in the array remove the same.

1 answer

7


You can turn it into an array using the implode() and then use the array_unique() to remove all duplicated words, and then return to normal string using implode().

Example:

$exemplo = "exemplo exemplo exemplo exemplo";
$exemplo = implode(" ", array_unique(explode(" ", $exemplo)));
echo $exemplo; // Saida  = "exemplo"
  • just to get this straight, vo explodes to transform into an array then uses Unique array and then returns to a string using implode

  • @Matheusrocha exactly.

  • I’m capturing the variable using a script( posted it in the post) and I think this is influencing , because the code n is working in my program.

  • 1

    @Matheusrocha you come with problem A and get answer to problem A, when you accept the answer can not reissue your question putting other problems, besides the question go without focus loses quality.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.