Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Wielki problem z array_unique
-Gość-
post 10.10.2008, 13:55:03
Post #1





Goście







Witam!

Mam w tabeli kolumnę, w której znajdują się dane oddzielone przecinkami, np. Polska, Niemcy.

W wielu wierszach, zarówno Polska, jak i Niemcy, się powtarzają.

  1. <?php
  2. $genere = $row['genere'];
  3. $array = explode(", ", $genere);
  4. $array = array_unique($array);
  5.            foreach ($array AS $name) {
  6.            echo $name;
  7.            }
  8. ?>


Mimo zastosowania $array_unique w tablicy w dalszym ciągu powtarzają się dane. Można to jakość obejść?
Go to the top of the page
+Quote Post
JoShiMa
post 10.10.2008, 14:58:47
Post #2





Grupa: Zarejestrowani
Postów: 1 374
Pomógł: 149
Dołączył: 1.03.2006

Ostrzeżenie: (0%)
-----


Widać te stringi nie są identyczne. Być może różnią się jakimiś pustymi znakami na początku lub końcu stringu (może przepuść przez trim()), albo wielkością liter. Z pewnością nie są identyczne.


--------------------
Go to the top of the page
+Quote Post
-Gość-
post 10.10.2008, 15:01:47
Post #3





Goście







Chyba nie o to chodzi. Bo jak ustawię stringi, to też mi nie idzie:)

  1. <?php
  2. $array = array('Heavy, Heavy, Metal');
  3. $array = array_unique($array);
  4.            foreach ($array AS $name) {
  5.            echo $name;
  6.            }
  7. ?>
Go to the top of the page
+Quote Post
wookieb
post 10.10.2008, 15:02:36
Post #4





Grupa: Moderatorzy
Postów: 8 989
Pomógł: 1550
Dołączył: 8.08.2008
Skąd: Słupsk/Gdańsk




zrob sobie
Kod
var_dump($array);

i pewnie zauwazysz ze niektore nazwy maja jakies puste znaki za i przed wlasciwym ciagiem.
Rozwiazaniem bedzie zastosowanie tego kodu przed array_unique.
Kod
$array=array_map('trim', $array);


--------------------
Go to the top of the page
+Quote Post
-Gość-
post 10.10.2008, 15:09:01
Post #5





Goście







Cytat(wookieb @ 10.10.2008, 15:02:36 ) *
zrob sobie
Kod
var_dump($array);

i pewnie zauwazysz ze niektore nazwy maja jakies puste znaki za i przed wlasciwym ciagiem.
Rozwiazaniem bedzie zastosowanie tego kodu przed array_unique.
Kod
$array=array_map('trim', $array);


Zaraz mnie pewnie zlinczujesz, ale są takie same. Spójrz z resztą:
  1. <?php
  2. array(2) { [0]=>  string(11) "Alternative" [1]=>  string(5) "Heavy" } array(2) { [0]=>  string(11) "Alternative" [1]=>  string(5) "Heavy" } array(1) { [0]=>  string(5) "Death" } array(2) { [0]=>  string(5) "Death" [1]=>  string(5) "Black" } array(2) { [0]=>  string(5) "Death" [1]=>  string(5) "Black" } array(2) { [0]=>  string(5) "Death" [1]=>  string(11) "Progressive" } array(2) { [0]=>  string(5) "Death" [1]=>  string(11) "Progressive" } array(1) { [0]=>  string(4) "Doom" } array(1) { [0]=>  string(9) "Folk Rock" } array(1) { [0]=>  string(4) "Glam" } array(1) { [0]=>  string(6) "Gothic" } array(3) { [0]=>  string(6) "Gothic" [1]=>  string(5) "Death" [2]=>  string(4) "Doom" } array(3) { [0]=>  string(6) "Gothic" [1]=>  string(5) "Death" [2]=>  string(4) "Doom" } array(3) { [0]=>  string(6) "Gothic" [1]=>  string(5) "Death" [2]=>  string(4) "Doom" } array(1) { [0]=>  string(9) "Hard Rock" } array(2) { [0]=>  string(9) "Hard Rock" [1]=>  string(5) "Heavy" } array(2) { [0]=>  string(9) "Hard
  3. ?>
Go to the top of the page
+Quote Post
drPayton
post 10.10.2008, 21:37:50
Post #6





Grupa: Zarejestrowani
Postów: 890
Pomógł: 65
Dołączył: 13.11.2005
Skąd: Olsztyn

Ostrzeżenie: (0%)
-----


Twoja tablica musi być "płaska" żeby array_unique mogło zadziałac w ten sposób, np:

$array = array('cos', 'cosinnego', 'cos', 'cos', 'foo', 'bar'); - zrób tu array_unique i zobaczysz że zadziała winksmiley.jpg
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.06.2025 - 19:51