Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klucz z tablicy wielowymiarowej
mk83
post
Post #1





Grupa: Zarejestrowani
Postów: 72
Pomógł: 0
Dołączył: 17.02.2005

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


Jak otrzymac klucz z tablic wielowymiarowej (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 11)
NuLL
post
Post #2





Grupa: Zarejestrowani
Postów: 2 262
Pomógł: 21
Dołączył: 3.05.2004
Skąd: Sopot, Krakow, W-wa

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


Co masz na mysli mowiac klucz - tablica wielowymiarowa napewno ma wiecej niz jeden.
Go to the top of the page
+Quote Post
mk83
post
Post #3





Grupa: Zarejestrowani
Postów: 72
Pomógł: 0
Dołączył: 17.02.2005

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


Chodzi o odpowiednik funkcji key, ktora zwraca klucz tablicy jednowymiarowej. Ja chcialbym zrobic cos podobnego tyle ze dla tablicy np 2-wymiarowej. Funkcja mialaby zwracac tablice w kotrej znajdowaly by sie klucze.
Go to the top of the page
+Quote Post
dtb
post
Post #4





Grupa: Zarejestrowani
Postów: 476
Pomógł: 1
Dołączył: 5.11.2005
Skąd: Bieruń city

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


foreach ($tablica as $key => $value)
Go to the top of the page
+Quote Post
mk83
post
Post #5





Grupa: Zarejestrowani
Postów: 72
Pomógł: 0
Dołączył: 17.02.2005

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


Moze to bedzie głupie pytanie ale co oznacz zapis $key => $value ?
Go to the top of the page
+Quote Post
NetJaro
post
Post #6





Grupa: Zarejestrowani
Postów: 475
Pomógł: 0
Dołączył: 1.04.2005
Skąd: Warszawa

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


$tablica['key'] = 'value';

Chyba jasne.
Go to the top of the page
+Quote Post
dtb
post
Post #7





Grupa: Zarejestrowani
Postów: 476
Pomógł: 1
Dołączył: 5.11.2005
Skąd: Bieruń city

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


  1. <?php
  2. $tbl = array ('name'=>'tresc', 'desc'=>'opis');
  3.  
  4. foreach ($tbl as $key => $value)
  5. {
  6. echo $key .' - '. $value .'<br/>';
  7. }
  8. ?>


zwróci:

name - nazwa
desc - opis

EDIT: NetJaro mnie uprzedzil

Ten post edytował dtb 3.12.2005, 12:08:09
Go to the top of the page
+Quote Post
mk83
post
Post #8





Grupa: Zarejestrowani
Postów: 72
Pomógł: 0
Dołączył: 17.02.2005

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


Dalej niestety mam problemy (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)
Napisałem sobie taką funkcje:

  1. <?php
  2.  
  3.  function multi_key($tab) {
  4.  foreach($tab as $key=>$value) {
  5.  $return[0]=$key;
  6.  foreach($value as $key2=>$value2) {
  7.  $return[1]=$key2;
  8.  break;
  9.  }
  10.  break;
  11.  }
  12.  return $return;
  13.  }
  14.  
  15. ?>


Funkcja ta zwraca mi tak jak chce klucz ale tylko pierwszego elementu z tablicy.
A mi chodzi o cos takiego:

  1. <?php
  2.  
  3. $tab[0][0]=1;
  4. $tab[0][1]=1;
  5. $tab[1][0]=2;
  6. $tab[1][1]=3;
  7.  
  8. ?>


  1. <?php
  2.  
  3.  for($i=0;$i<2;$i++) {
  4.  for($j=0;$j<2;$j++) {
  5.  if($tab[$i][$j]==1) {
  6.  $x=multi_key($tab);
  7.  print("[$x[0]][$x[1]]<br>"); }
  8.  }
  9.  }
  10.  
  11. ?>


Chodzi o to ze gdy przgladam sobie po koleji wszystkie elementy tablicy i trafie na 1 to ma wypisac sie klucz ktory danemu elementowi odpowiada. Tu moja funkcja sie nie sprawdza poniewaz zawsze zwraca klucz pierwszego elementu. Jak zrobic aby działała ona nie od poczatku tablicy ale od elementu ktory jest akutalnie przegladany w petlach for ?
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #9





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


KOD JEST NIEPRAWIDLOWY!!!!!!! nowy juz sie pisze...

  1. <?php
  2. $table = array( 'x' => array( 'A' => 65, 'B' => 66 ), 'z' => array( 'a' => 95, 'b' => 96, 'c' => 99 ) );
  3.  
  4. function getAllKeysRecursivly( $arrTable )
  5. {
  6.     if ( is_array( $arrTAble ) )
  7.     {
  8.         $arrReturnTable = array();
  9.         
  10.         foreach ( $arrTable as $key => $value ) // BUGFIX: 'arrTAble' => 'arrTable', tak to jest jak sie nie ustawi errorow na E_ALL
  11.         {
  12.             $arrReturnTable[ $key ] = getAllKeysRecursivly( $arrTable );
  13.         }
  14.         
  15.         return $arrReturnTable;
  16.     }
  17.     else
  18.     {
  19.         return $arrTable; // wzwraca pojedynczy element tablicy
  20.     }
  21.     
  22. }
  23.  
  24. print_r( getAllKeysRecursivly( $table ) );
  25. ?>

Pozdro od wrozki rekurencji (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) EDIT:: // co za porazka, ehhh

Ten post edytował dr_bonzo 3.12.2005, 16:07:38
Go to the top of the page
+Quote Post
dtb
post
Post #10





Grupa: Zarejestrowani
Postów: 476
Pomógł: 1
Dołączył: 5.11.2005
Skąd: Bieruń city

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


twoja funkcja mija sie z celem bo $table === getAllKeysRecursivly ($table)

zwraca dokladnie to co chcesz:

  1. <?
  2. function wymiary ($tbl, $pocz='$tbl')
  3. {
  4. foreach ($tbl as $key => $val)
  5. if (!is_array ($val)) echo $pocz.'['.$key.'] = '.$val.'<br/>';
  6. else wymiary ($val, $pocz.'['.$key.']');
  7. }
  8.  
  9. wymiary ($tablica);
  10. ?>


Ten post edytował dtb 3.12.2005, 14:55:09
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #11





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

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


Zgadza sie -- nie sprawdzilem dzialania skryptu.

  1. <pre><?php
  2. $table2 = array( array( 'A' => 65, 'B' => 66 ), array( 'a' => 95, 'b' => 96, 'c' => 99 ), array( 'm' => array( 'f' => 0, 'g' => 1, 'h' => 2 ), 'n' =>array( 'f' => 0, 'g' => 1, 'h' => 2 ) ) );
  3.  
  4. function getAllKeysRecursively( $arrTable )
  5. {
  6. $arrReturnTable = array();
  7. foreach ( $arrTable as $key => $value )
  8. {
  9. if ( ! is_array( $value ) )
  10. {
  11. $arrReturnTable[] = $key;
  12. }
  13. else
  14. {
  15. $arrReturnTable[ $key ] = getAllKeysRecursively( $value );
  16. }
  17. }
  18.  
  19. return $arrReturnTable;
  20.  
  21. }
  22.  
  23. print_r( $table2 );
  24. $res = getAllKeysRecursively( $table2 );
  25. print_r( $res );
  26. ?></pre>

Rezultat:
Kod
Array
(
    [0] => Array
        (
            [0] => A
            [1] => B
        )

    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [2] => Array
        (
            [m] => Array
                (
                    [0] => f
                    [1] => g
                    [2] => h
                )

            [n] => Array
                (
                    [0] => f
                    [1] => g
                    [2] => h
                )

        )

)
Go to the top of the page
+Quote Post
dtb
post
Post #12





Grupa: Zarejestrowani
Postów: 476
Pomógł: 1
Dołączył: 5.11.2005
Skąd: Bieruń city

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


Kod
$tbl[x][A] = 65
$tbl[x][B] = 66
$tbl[z][a] = 95
$tbl[z][b] = 96
$tbl[z][c] = 99


rezualtat mojego kodu
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: 25.12.2025 - 02:55