Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Unikalne wartości, z tablicy dwuwymiarowej
-merrit-
post 13.08.2008, 23:41:22
Post #1





Goście







Jak uzyskać tablicę bez powtarzających się wartości [link] w następującej tablicy?

Kod
Array
(
    [0] => Array
        (
            [title] => Some feed item
            [link] => http://www.example.com/article34
            [description] => This article is really cool!
            [author] => Aart-Jan Boor
            [pubDate] => Sat, 08 Dec 2007 13:28:11 GMT
        )

    [1] => Array
        (
            [title] => Some feed item2
            [link] => http://www.example.com/article34
            [description] => This article is really cool too!
            [author] => Aart-Jan Boor
            [pubDate] => Sat, 08 Dec 2007 12:57:56 GMT
        )

    [2] => Array
        (
            [title] => Some feed item3
            [link] => http://www.example.com/article4523
            [description] => This article is the best!
            [author] => Aart-Jan Boor
            [pubDate] => Sat, 08 Dec 2007 12:39:42 GMT
        )

)
Go to the top of the page
+Quote Post
strife
post 14.08.2008, 00:15:51
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 605
Pomógł: 96
Dołączył: 22.10.2004
Skąd: UK

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


array_unique" title="Zobacz w manualu PHP" target="_manual


--------------------
Go to the top of the page
+Quote Post
wookieb
post 14.08.2008, 00:17:01
Post #3





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




Bawisz się z array_unique badz...
Kod
function deleteDuplicate($tab)
{
$unique=array();
$ile=count($tab); // KONIECZNE!
for($i=0; $i<$ile; $i++)
{
    if(!in_array($tab[$i]['link'], $unique) $unique[]=$tab[$i]['link']
    else unset($tab[$i]);
}

// jeżeli chcesz jeszce zresetować klucze na koniec to daj $tab=array_merge($tab);
return $tab;
}

print_r(deleteDuplicate($tab));


Tablica wynikowa to:
Kod
Array
(
    [0] => Array
        (
            [title] => Some feed item
            [link] => http://www.example.com/article34
            [description] => This article is really cool!
            [author] => Aart-Jan Boor
            [pubDate] => Sat, 08 Dec 2007 13:28:11 GMT
        )

    [2] => Array
        (
            [title] => Some feed item3
            [link] => http://www.example.com/article4523
            [description] => This article is the best!
            [author] => Aart-Jan Boor
            [pubDate] => Sat, 08 Dec 2007 12:39:42 GMT
        )

)


--------------------
Go to the top of the page
+Quote Post
-merrit-
post 14.08.2008, 20:25:50
Post #4





Goście







array_unique próbowałem zanim jeszcze zapytałem na forum, nie działa tak jak należy, zwraca z tablicy tylko jedną wartość

co do tej funkcji

Kod
function deleteDuplicate($tab)
{
$unique=array();
$ile=count($tab); // KONIECZNE!
for($i=0; $i<$ile; $i++)
{
if(!in_array($tab[$i]['link'], $unique) $unique[]=$tab[$i]['link'] // --------wywala błąd w tej linii
    else unset($tab[$i]);
}

// jeżeli chcesz jeszce zresetować klucze na koniec to daj $tab=array_merge($tab);
return $tab;
}


Parse error: syntax error, unexpected T_VARIABLE in /home/xxx/domains/xxx/public_html/test/xx.php on line xxx
Go to the top of the page
+Quote Post
wookieb
post 14.08.2008, 22:48:54
Post #5





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




Dodaje na jej koncu srednik......


--------------------
Go to the top of the page
+Quote Post
-merrit-
post 15.08.2008, 06:59:20
Post #6





Goście







Cytat
Dodaje na jej koncu srednik......


hehe.. nie, to nie to ..

poza tym jak się zastanowić chwilę to rozwiązanie nie może działać właściwie, do nowej tablicy wpisywany byłby jedynie [$i]['link']..

dobre dzięki za dobre chęci jakoś inaczej sobię poradzę, pozdrawiam..
Go to the top of the page
+Quote Post
wookieb
post 15.08.2008, 07:34:56
Post #7





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




To widze, że ty się nie "zastanawiasz" tylko odrazu walisz takie głupoty.
Nawet nie umiesz poprawić kodu.
Kod
<?php
function deleteDuplicate($tab)
{
$unique=array();
$ile=count($tab); // KONIECZNE!

for($i=0; $i<$ile; $i++)
{
    if(!in_array($tab[$i]['link'], $unique)) $unique[]=$tab[$i]['link'];
    else unset($tab[$i]);
}

$tab=array_merge($tab);
return $tab;
}


$tab=Array
(
    Array
    (
        'title'=>'Some feed item',
        'link'=>'http://www.example.com/article34',
        'description' => 'This article is really cool!',
        'author' => 'Aart-Jan Boor',
        'pubDate' => 'Sat, 08 Dec 2007 13:28:11 GMT'
    ),

    Array
    (
        'title' => 'Some feed item2',
        'link' => 'http://www.example.com/article34',
        'description' => 'This article is really cool too!',
        'author' => 'Aart-Jan Boor',
        'pubDate' => 'Sat, 08 Dec 2007 12:57:56 GMT'
    ),

    Array
    (
        'title' => 'Some feed item3',
        'link' => 'http://www.example.com/article4523',
        'description' => 'This article is the best!',
        'author' => 'Aart-Jan Boor',
        'pubDate' => 'Sat, 08 Dec 2007 12:39:42 GMT'
    )

);

print_r(deleteDuplicate($tab));
?>

I sprawdź sobie "wielki myślicielu" czy to nie działa poprawnie.


--------------------
Go to the top of the page
+Quote Post
-merrit-
post 15.08.2008, 08:04:46
Post #8





Goście







Cytat
To widze, że ty się nie "zastanawiasz" tylko odrazu walisz takie głupoty.
Nawet nie umiesz poprawić kodu.


Pytanie, kto te błędy zrobił, i po co wklejać komuś kod z błędami.

Mimo wszystko dziękuję.
Go to the top of the page
+Quote Post
wookieb
post 15.08.2008, 08:16:35
Post #9





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




To było założenie ideologiczne.


--------------------
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: 13.07.2025 - 01:18