5. Juli 200421 j hi und hallo, kann mir jemand erklären wieso hier "false" ausgegeben wird?? $arrTest = Array(0,1,2,3,4); $key = array_search(0,$arrTest); if($key == NULL){ echo("false"); } denn "0" findet sich doch im array. nämlich an stelle [0]. wenn ich nach 1 oder 2 oder .... suche wird mir nicht false ausgegeben, die zahlen werden also gefunden.
5. Juli 200421 j Verwende doch mal den typsicheren Vergleichsoperator "===". Sonst wird 0 als null interpretiert.
5. Juli 200421 j also if($key === null){ echo("false"); } oder if($key === 0){ echo("false"); } ? wenn ich dann aber nach -1 suche wird mir nicht false ausgegeben obwohl sich -1 ja nicht im array findet...
5. Juli 200421 j if ($key === false) { // wenn php-version < 4.2.0 dann $key === null echo "suche blieb ohne ergebnis <br />"; } [/PHP]
5. Juli 200421 j siehe manual: Description mixed array_search ( mixed needle, array haystack [, bool strict]) Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise. Note: If needle is a string, the comparison is done in a case-sensitive manner. Note: Prior to PHP 4.2.0, array_search() returns NULL on failure instead of FALSE. If the optional third parameter strict is set to TRUE then the array_search() will also check the types of the needle in the haystack. 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. $arrTest = Array(0,1,2,3,4); $key = array_search(0,$arrTest); if(!$key){ echo "Da ist nichts drinn?!?"; } else { echo "Juhu da ist doch was :)"; } [/PHP] oder: [PHP] $arrTest = Array(0,1,2,3,4); $key = array_search(0,$arrTest); if($key === false){ echo "Da ist nichts drinn?!?"; } else { echo "Juhu da ist doch was :)"; }
Archiv
Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.