Group tags separated by PHP commas

0

Good morning!

Next I have a question, and I am not able to reach conclusion.

I'm creating a field called tag where it fetches tag related posts.

In the bank it is filled as follows;

post1 tag: exemplo1,exemplo2
post2 tag: exemplo3,exemplo2

And how it should be output:

tag: exemplo1,exemplo2,exemplo3

If repaired well, it is separated by a comma. So I make an explode to separate string. So far so good.

My question is in the string part repeating the same.

$query = mysqli_query($con, "SELECT tag FROM posts WHERE tag NOT IN('') ");

while($row = mysqli_fetch_array($query)){
  $explode = explode(',',$row['tag']);
  $count = count($explode);
  for($i = 0; $i < $count-1; $i++){
    echo $explode[$i];
  }
}

With this code it comes out as follows:

tag: exemplo1,exemplo2,exemplo3,exemplo2

Which way to group repeated string?

    
asked by anonymous 18.06.2017 / 09:50

2 answers

0

$raw = 'exemplo1,exemplo2,exemplo3,exemplo2';
$string = implode(',', array_unique(explode(',', $raw)));
echo $string; // output: exemplo1,exemplo2,exemplo3

I've done something like this for a while, and it's still a reference if it helps: selecting-hashtag-into-database-from-url

    
18.06.2017 / 10:11
0

For those who want the code.

$query = mysqli_query($con, "SELECT tag FROM posts WHERE tag NOT IN ('')");

while($row = mysqli_fetch_array($query)){
    $explode = explode(',',$row['tag']);
    $count = count($explode);
    for ($i = 0; $i < $count; $i++) {
        $array .= $explode[$i].',';
    }
}

echo implode(',', array_unique(explode(',', $array)));
    
18.06.2017 / 10:33