1

I try using array_unique(); but not working this is my code please help me what i doing wrong? I want display all tags without duplicates

ID | TAGS
1 | rock, punk, jazz
2 | pop, rock, classic
3 | jazz, blues, rock
4 | rock, rap, metal

$wynik = mysql_query("SELECT * FROM nabk_t_item_tags") or die('Błąd zapytania');
if (mysql_num_rows($wynik) > 0) {
    while ($r = mysql_fetch_assoc($wynik)) {
        $input = $r['tags'];
        $fields = explode(',', $input);
        $fields2 = array_unique($fields);
        foreach ($fields2 as $field) {
            echo '"' . $field . '",';
        }
    }
}
Tomasz
  • 27
  • 8
  • 5
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 18 '17 at 00:33
  • 3
    Start by showing us an example of what is contained in the `tags` column. Then describe what is wrong with your existing code that needs fixing – RiggsFolly Jan 18 '17 at 00:34
  • sorry please elaborate, what are you expecting and what is it doing? You want to only receive one of each value? – Craig B Jan 18 '17 at 00:48
  • I want display all tags without duplicates (rock, punk, jazz, pop, classic, metal etc.) ID | TAGS 1 | rock, punk, jazz 2 | pop, rock, classic 3 | jazz, blues, rock 4 | rock, rap, metal – Tomasz Jan 18 '17 at 02:27
  • You should add each tag in new row, not comma separated. Then things will get real easy. That table won't insert duplicates and you only have to do `select * from tags` – Abdul Rehman Jan 18 '17 at 07:03

2 Answers2

1

You have to put all the tags from the query in one single array. So, using your existing code:

// $fields2 = array_unique($fields);
$fields2 = array_merge($fields2, $fields);

Then after the while loop, $fields2 will have all the tags and you can array_unique that array. So, add these lines and see the result:

    } // end while
    $unique_tags = array_unique($fields2);
    var_dump($unique_tags);
} // end if
imel96
  • 1,385
  • 11
  • 18
  • Your solution is good - for someone who worked with PHP (obviously he didn't), therefore he may not understand it. Edit your answer with a full working code and comments, and he might get it. – besciualex Jan 18 '17 at 06:59
  • @besciualex thanks, edited to make it easier to understand – imel96 Jan 18 '17 at 07:10
0

Try this code.

if (mysql_num_rows($wynik) > 0) {
    $used=array();
    while ($r = mysql_fetch_assoc($wynik)) {
        $input = $r['tags'];
        $fields = explode(',', $input);
        foreach($fields as $tg){
            if(!isset($used[$tg])){
                echo '"' . $tg . '",';
                $used[$tg]=" ";
            }   
        }
    }
    unset($used);
}
Confused
  • 1,602
  • 15
  • 25