-2

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
masud_moni
  • 1,121
  • 16
  • 33
peter Bou
  • 39
  • 1
  • 7
  • Be careful with carriage return in your `$tags` string. – Sir McPotato Mar 03 '17 at 10:22
  • You are doing a lot of mistzakes, your code does not run without bugfixing. Go through the errors and you will see. Specially take a look at **...array_unique() expects parameter 1 to be array, string given** And change _$resul_ to _$result_ – B001ᛦ Mar 03 '17 at 10:23
  • @bub please help me, i have ben trying to get this right for over 2 weeks now – peter Bou Mar 03 '17 at 10:24

2 Answers2

0
$tags = 'html,php,css,jquery,xml,js,php,python,xhtml,css'; //string
$string = explode(',', $tags); //convert string into array by using php standard function
echo "<pre>"; print_r($string); // print array
$string1 = array_unique($string); //Removes duplicate values from an array
echo "<pre>"; print_r($string1); //print unique array
// if you want string again of this array use implode function
$unique_tags = implode(',', $string1);
echo $unique_tags;
Bhaskar Jain
  • 1,651
  • 1
  • 12
  • 20
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Mar 03 '17 at 12:12
0

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>";

}