开发者

how to show unique array values in php

开发者 https://www.devze.com 2022-12-21 22:13 出处:网络
$content = \'<h3>popular tags »</h3><hr>\'; $result = $db->sql_query(\"SELECT tags FROM \".DOWNLOAD_TABLE.\" \");
   $content = '<h3>popular tags »</h3><hr>';

    $result = $db->sql_query("SELECT tags FROM ".DOWNLOAD_TABLE." ");
    while($row = $db->sql_fetchrow($result)){
     $dl_tags_id = $row['tags'];
     $dl_tags_id_ex = explode(" ",$dl_tags_id)   ;
     $dl_tags_id = array_unique($dl_tags_id_ex);

     $c = count($dl_tags_id);
     for($i=1;$i<$c-1;$i++){
//for loop content

     }
    }
    echo 开发者_Go百科$content;

Hey guys. I used the above code to show my popular tags. As you can see in the code, I have two tables, one is download table that stores tags ID in array format such as:

20 21 13 14

And the other one is tags table that define those IDs

Now the only problem is that when printing this code, I can see duplicated tags as:

white(2)
blue(4)
white(2)

I wonder how prevent showing duplicated output.

Thanks in advance


this is happening because you're doing the array_unique call on each row!

You're doing this:

while($row = $db->sql_fetchrow($result)){
    $dl_tags_id = $row['tags'];
    $dl_tags_id_ex = explode(" ",$dl_tags_id)   ;
    $dl_tags_id = array_unique($dl_tags_id_ex);
    // rest of code...
}

when really, you want to do this:

$dl_tags_all = '';
while($row = $db->sql_fetchrow($result)){
    $dl_tags_all .= ' '.$row['tags'];
}
$dl_tags_id_ex = explode(" ",substr($dl_tags_all,1));
$dl_tags_id = array_unique($dl_tags_id_ex);
//rest of code....
0

精彩评论

暂无评论...
验证码 换一张
取 消