Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Problem z tablicą
Glarden
post
Post #1





Grupa: Zarejestrowani
Postów: 153
Pomógł: 2
Dołączył: 28.05.2007
Skąd: Stamtąd

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


Witam
ma problem z operacją na tablicy.
Załóżmy, że mam tablicę:
1 => 'Kot'
2 => 'Pies'
3 => 'Krowa'
Jest jakaś funkcja lub sposób, żeby zyskać indeks np. kota? Tak, żeby - w tym wypadku - zwróciło 1?


--------------------
Raz, dwa, trzy, baba Jaga patrzy!
http://trelemorele.cba.pl
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 8)
Cysiaczek
post
Post #2





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




  1. <?php
  2. print array_keys($array, 'pies');
  3. ?>


Ten post edytował Cysiaczek 8.06.2007, 14:36:33


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
bigZbig
post
Post #3





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


http://pl.php.net/manual/pl/function.array-search.php


--------------------
bigZbig (Zbigniew Heintze) | blog.heintze.pl
Go to the top of the page
+Quote Post
zombie
post
Post #4





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

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


Chyba raczej array_search


--------------------
audaces fortuna iuvat!
Go to the top of the page
+Quote Post
Cysiaczek
post
Post #5





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Z ta róznicą, że array_keys zwróci wszystkie wystąpienia jako tablicę.
  1. <?php
  2. $arr=array("kot", "pies", 'kot', 'pupilek'=>kot);
  3. print_r (array_keys($arr, 'kot'));
  4. // Array ( [0] => 0 [1] => 2 [2] => pupilek )
  5. ?>


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
Cezar708
post
Post #6





Grupa: Zarejestrowani
Postów: 1 116
Pomógł: 119
Dołączył: 10.05.2005
Skąd: Poznań

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


niestety chyba tylko pozostaje:

  1. <?php
  2. function get_key_by_value($arr, $myvalue){
  3. foreach ( $arr as $key => $value ){
  4. if ( $value == $myvalue ) {
  5.  return $key;
  6. }
  7. }
  8. }
  9.  
  10.  
  11. $arr = array('kot','pies','krowa');
  12. echo get_key_by_value($arr, 'pies');
  13. ?>
Go to the top of the page
+Quote Post
Cysiaczek
post
Post #7





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




@Cezar708
Prózny Twój trud - istnieją (jak podaliśmy) wbudowane funkcje, które to zrobią. : >


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
Glarden
post
Post #8





Grupa: Zarejestrowani
Postów: 153
Pomógł: 2
Dołączył: 28.05.2007
Skąd: Stamtąd

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


Mam jeszcze jedno pytanie, spytam tutaj.
Jak usunąć zmienną z tablicy, np. kota?
bo:
unset($array[1]);
nie działa...


--------------------
Raz, dwa, trzy, baba Jaga patrzy!
http://trelemorele.cba.pl
Go to the top of the page
+Quote Post
Sabistik
post
Post #9


Administrator wortalu


Grupa: Przyjaciele php.pl
Postów: 960
Pomógł: 39
Dołączył: 21.10.2003
Skąd: Kraków

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


Proszę o zapoznanie się z tematem: Temat: Tematyka i zasady panujące na forum Przedszkole a następnie o PW z poprawnym tematem.

Do tego czasu zamknięte.

Otwieram.
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 Aktualny czas: 19.08.2025 - 03:16