Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] tablica w tablicy
bastek8989
post
Post #1





Grupa: Zarejestrowani
Postów: 205
Pomógł: 1
Dołączył: 11.09.2006

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


Witam

Chodzi mi oto czy da sie zrobic cos takeigo

$tablica[1][wyjdzie zawartosc tablicy
$tablica[1][id] wyjdzie ID

jest ajkis sposob?
Go to the top of the page
+Quote Post
Kicok
post
Post #2





Grupa: Zarejestrowani
Postów: 1 033
Pomógł: 125
Dołączył: 17.09.2005
Skąd: Żywiec

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


each" title="Zobacz w manualu PHP" target="_manual ewentualnie możesz poczytać jeszcze o funcjach key() i current()
Go to the top of the page
+Quote Post
bastek8989
post
Post #3





Grupa: Zarejestrowani
Postów: 205
Pomógł: 1
Dołączył: 11.09.2006

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


i jak niby za pomoca tego moge zrobic tablice o 2 wartosciach (IMG:http://forum.php.pl/style_emoticons/default/sleep.gif)
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




ale oco ci chodzi? Bo Twoj opis, a tym bardziej przyklad jest "cudny" (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

  1. <?php
  2. $tablica = array(1=>array('id'=>2, 'inny index'=>3));
  3. ?>

oto chodzi?
Go to the top of the page
+Quote Post
Kicok
post
Post #5





Grupa: Zarejestrowani
Postów: 1 033
Pomógł: 125
Dołączył: 17.09.2005
Skąd: Żywiec

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


Zawsze można opisywać problem w taki sposób, żeby był czytelny dla innych...

ew. array_values() i array_keys()
Go to the top of the page
+Quote Post
bastek8989
post
Post #6





Grupa: Zarejestrowani
Postów: 205
Pomógł: 1
Dołączył: 11.09.2006

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


Cytat(nospor @ 28.02.2007, 17:25:16 ) *
ale oco ci chodzi? Bo Twoj opis, a tym bardziej przyklad jest "cudny" (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

  1. <?php
  2. $tablica = array(1=>array('id'=>2, 'inny index'=>3));
  3. ?>

oto chodzi?



tak! a jak moge wsadzic dane do tej nietypowej tablicy o_O

zebym mogl je wywolac:

$tablica[1] otrzmuje wartosc 1
$tablica[1][2] otrzymuje wartosc 2?

chodzi o masowe wsadzanie danych (przy 1 tablicy to bylo $tablica[] = 'asdasd';) jak przy tej

Ten post edytował bastek8989 28.02.2007, 17:40:09
Go to the top of the page
+Quote Post
nospor
post
Post #7





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




  1. <?php
  2. $tablica = array(1=>array('id'=>2, 'inny index'=>3));
  3. //i dodajemy nowe dane
  4. $tablica[2]=array('id'=>2,'inne'=>3);
  5. //lub
  6. $tablica[3]['id']=2;
  7. $tablica[3]['inne']=3;
  8. ?>

(IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
bastek8989
post
Post #8





Grupa: Zarejestrowani
Postów: 205
Pomógł: 1
Dołączył: 11.09.2006

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


SUPER ale mam problem ;d;d;d


oto kod:

  1. <?php
  2. //sprawdza czy tablica nie jest pusta
  3. function myMerge($arrays){
  4. $moja = array();
  5. foreach($arrays as $array){
  6. if(is_array($array)) $moja = array_merge($moja, $array);
  7. }
  8. return $moja;
  9. }
  10.  
  11. //te tablice sa w petli wybieraja dane z bazy
  12. $tablica_filmy = array(1=>array('tabela'=>'filmy', 'id'=>$wynikf['id']));
  13. $tablica_mp3 = array(1=>array('tabela'=>'mp3', 'id'=>$wynikf['id']));
  14. $tablica_flash = array(1=>array('tabela'=>'flash', 'id'=>$wynikf['id']));
  15. $tablica_galeria = array(1=>array('tabela'=>'galeria', 'id'=>$wynikf['id']));
  16.  
  17. //polaczenie i pomieszanie tablic
  18. $losowe = myMerge(Array($tablica_galeria,$tablica_filmy, $tablica_flash, $tablica_mp3));
  19. if($losowe != NULL){
  20. shuffle($losowe);
  21. ?>


No i dziala on polowicznie :|

$losowe[1]['tabela'] <<< dziala
$losowe[1]['tabela']['id'] <<< niestety nie dziala tablica sie zatracila gdzies :|

dodam jeszcze zawartosc tablicy $losowe:

array(4) { [0]=> array(2) { ["tabela"]=> string(3) "mp3" ["id"]=> string(3) "254" } [1]=> array(2) { ["tabela"]=> string(7) "galeria" ["id"]=> string(3) "381" } [2]=> array(2) { ["tabela"]=> string(5) "filmy" ["id"]=> string(3) "635" } [3]=> array(2) { ["tabela"]=> string(5) "flash" ["id"]=> string(3) "221" } }

Ten post edytował bastek8989 28.02.2007, 18:04:11
Go to the top of the page
+Quote Post
nospor
post
Post #9





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




no ale przeciez twoj kod wygeneruje ci takie tablice:
Kod
Array
(
    [0] => Array
        (
            [tabela] => galeria
            [id] => jakas wartosc
        )

    [1] => Array
        (
            [tabela] => filmy
            [id] => jakas wartosc
        )

    [2] => Array
        (
            [tabela] => flash
            [id] => jakas wartosc
        )

    [3] => Array
        (
            [tabela] => mp3
            [id] => jakas wartosc
        )

)

wiec nie $losowe[1]['tabela']['id'] a: $losowe[1]['id']
Go to the top of the page
+Quote Post
bastek8989
post
Post #10





Grupa: Zarejestrowani
Postów: 205
Pomógł: 1
Dołączył: 11.09.2006

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


Bardzo dziekuje!

Nareszcie normalne odpowiedzi a nie odsylacze do funkcji w manualu i google (IMG:http://forum.php.pl/style_emoticons/default/haha.gif)


pozdro

Jeszcze jeden problem napotkalem:

Moj kod dodajacy dane do tablicy w petli:
  1. <?php
  2. if($ileobrazki != 0){
  3. $i_ob = 0;
  4. $sql2 = "SELECT * FROM galeria ORDER BY RAND() LIMIT 3";
  5. $result2 = mysql_query($sql2);
  6. while($wynik2=mysql_fetch_array($result2))
  7. {
  8. $tablica_galeria = array($i_ob=>array('tabela'=>'galeria', 'id'=>$wynik2['id']));
  9. $i_ob++;
  10. }
  11. }
  12. ?>


Pętla dziala lecz niestety do tablicy dodawany jest tylko 1 element :| prosze o rade

Ten post edytował bastek8989 1.03.2007, 13:21:31
Go to the top of the page
+Quote Post
nospor
post
Post #11





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




  1. <?php
  2. $tablica_galeria[] = array($i_ob=>array('tabela'=>'galeria', 'id'=>$wynik2['id']));
  3. ?>

Oto chodzi?
A moze oto:
  1. <?php
  2. $tablica_galeria[$i_ob] = array('tabela'=>'galeria', 'id'=>$wynik2['id']);
  3. ?>

?
Go to the top of the page
+Quote Post
bastek8989
post
Post #12





Grupa: Zarejestrowani
Postów: 205
Pomógł: 1
Dołączył: 11.09.2006

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


O to drugie - dziekuje
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 24.08.2025 - 10:49