Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Problem z array_search()
cuube
post
Post #1





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 21.06.2006

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


Mam taki oto kod:

  1. <?php
  2. abstract class AbstractInstrument {
  3. private $name;
  4. private $category;
  5. private $instruments = array();
  6.  
  7. public function getName() {
  8. return $this->name;
  9. }
  10.  
  11. public function add (AbstractInstrument $instrument) {
  12. array_push ($this->instruments, $instrument);
  13. }
  14.  
  15. public function checkFamily() {
  16. $check = array_search($this->getName(), $this->instruments);
  17. if ($check === FALSE) { echo "brak"; } else { echo $check; }
  18. }
  19. }
  20. ?>


Dodaj sobie instrumenty itd. Tylko nie rozumiem dlaczego wywołując funkcje checkFamily() zawsze zwraca mi false - nawet jeśli istnieje w nim string z getName().

Z góry dzięki za pomoc.

Ten post edytował cuube 11.07.2008, 11:47:07
Go to the top of the page
+Quote Post
batman
post
Post #2





Grupa: Moderatorzy
Postów: 2 921
Pomógł: 269
Dołączył: 11.08.2005
Skąd: 127.0.0.1




Domyślam się, że w tablicy nie ma wartości $this->getName(). Nie wartości zwracanej przez funkcję, a wyrażenia $this->getName().
Zrób tak:
  1. <?php
  2. $check = array_search($this->getName(), $this->instruments);
  3. ?>
Go to the top of the page
+Quote Post
cuube
post
Post #3





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 21.06.2006

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


Tzn nawet jeśli wstawię:
  1. <?php
  2. $check = array_search('a', $this->instruments);
  3. ?>

to i tak mi nic nie zwraca. Ponadto print_r ($this->getName()) oraz $this->instruments ładnie zwraca. :/
Go to the top of the page
+Quote Post
batman
post
Post #4





Grupa: Moderatorzy
Postów: 2 921
Pomógł: 269
Dołączył: 11.08.2005
Skąd: 127.0.0.1




A co masz w zmiennej $name? Gdzie ją ustawiasz?
Go to the top of the page
+Quote Post
cuube
post
Post #5





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 21.06.2006

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


  1. <?php
  2. abstract class AbstractInstrument {
  3. private $name;
  4. // ...
  5. public function setName ($name) {
  6. $this->name = $name;
  7. }
  8.  
  9. public function getName() {
  10. return $this->name;
  11. }
  12. // ...
  13. }
  14. ?>

Aha - w tablicy jest wartość zwracana przez $this->getName().

Ten post edytował cuube 11.07.2008, 11:57:53
Go to the top of the page
+Quote Post
batman
post
Post #6





Grupa: Moderatorzy
Postów: 2 921
Pomógł: 269
Dołączył: 11.08.2005
Skąd: 127.0.0.1




Spróbuj użyć in_array i jeśli to też nie zadziała, to użyj funkcji trim na danych zwracanych przez getName().
Go to the top of the page
+Quote Post
cuube
post
Post #7





Grupa: Zarejestrowani
Postów: 8
Pomógł: 0
Dołączył: 21.06.2006

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


Działa z in_array! Dzięki. (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Bez sensu... Nic nie zmieniałem, a teraz znowu nie działa. :/ Albo ja jestem głupi, albo nie wiem co, ale błędu znaleść jak nie mogłem tak dalej nie mogę. :/

Ten post edytował cuube 11.07.2008, 12:57:48
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 - 21:12