Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Przeszukiwanie tablicy nie zwraca wszystkich wyników
mlody69
post
Post #1





Grupa: Zarejestrowani
Postów: 183
Pomógł: 0
Dołączył: 18.05.2009

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


Witam,
posiadam następującą funkcję, która przeszukuje mi daną tablicę. PRzykładowe dane:
  1. Array
  2. (
  3. [6] => Array
  4. (
  5. [rola] => aktor
  6. [name] => SebastiĂĄn Molinaro
  7. )
  8.  
  9. [7] => Array
  10. (
  11. [rola] => aktor
  12. [name] => Marta Lubos
  13. )
  14.  
  15. [8] => Array
  16. (
  17. [rola] => aktor
  18. [name] => Valentina GarcĂ­a Guerrero
  19. )
  20.  
  21. [9] => Array
  22. (
  23. [rola] => aktor
  24. [name] => Silvia BaylĂŠ
  25. )
  26.  
  27. [10] => Array
  28. (
  29. [rola] => aktor
  30. [name] => Sofia Palomino
  31. )
  32.  
  33. [11] => Array
  34. (
  35. [rola] => aktor
  36. [name] => Sandra Villani
  37. )
  38.  
  39. [12] => Array
  40. (
  41. [rola] => aktor
  42. [name] => Paula Ituriza
  43. )
  44.  
  45. [13] => Array
  46. (
  47. [rola] => aktor
  48. [name] => Carlos Weber
  49. )

Funkcja:
  1. function search($array, $role) {
  2.  
  3. $key = array_search($role, array_column($array, 'rola'));
  4. $name = $array[$key][name];
  5.  
  6. return $name;
  7.  
  8. }

Problem polega na tym, że ta funkcja zwraca tylko pierwszy wynik a nie wszystkie możliwe i kompletnie nie wiem jak to zmienic.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
nospor
post
Post #2





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




W manualu przy funkcji array_key masz wyraźnie napisane:
Cytat
If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

Manuala naprawde po cos wymyslono


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
mlody69
post
Post #3





Grupa: Zarejestrowani
Postów: 183
Pomógł: 0
Dołączył: 18.05.2009

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


Dziekuje, pomogło
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 - 04:03