Search code examples
phparray-unique

array unique not working fine on a tag string


I have been battling on how to display distinct tag that I stored in database. In my database I have saved some tags, when I tried displaying it back I used array unique but it didn't work. see below example database

item     | tag
---------|---------------
code 1   | html,php,css
code 2   | jquery,xml,js
code 3   | php,python,xhtml
code 4   | css

Now when i select tag from my table i will get below

<?php
    $tags = 'html,php,css
             jquery,xml,js
             php,python,xhtml
             css';
//Here i 

    $string = explode(',', $tags);
    foreach($string as $linetag){
        //echo $linetag;
        $result = array_unique($linetag);
        echo $resul;
    }
?>

But the above code is not working. I want to display unique tags and remove duplicates like below

html,
php,
css
jquery,
xml,
js
python,
xhtml

Solution

  • Try this for href anchor tag

     $tags = 'html,php,css,jquery,xml,js,php,python,xhtml,css';
    
           $string = explode(',', $tags);
           $result = array_unique($string);
    
      foreach($result as $d){
    
      echo ' '."<a href=''> $d</a>";
    
    }