数组唯一在标记字符串上不能正常工作

时间:2017-03-03 10:19:47

标签: php array-unique

我一直在努力研究如何显示我存储在数据库中的不同标记。在我的数据库中,我保存了一些标签,当我尝试显示它时,我使用了数组唯一但它没有用。见下面的示例数据库

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

现在当我从我的桌子中选择标签时,我会在下面

<?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;
    }
?>

但上面的代码不起作用。我想显示唯一标签并删除重复项,如下所示

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

尝试使用href锚标记

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

}
相关问题