Re: how to make array_search start from 0?

From: Date: Thu, 25 Apr 2002 17:38:55 +0000
Subject: Re: how to make array_search start from 0?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-94597@lists.php.net to get a copy of this message
I can see what u are saying. Its all about the === Anyhow.. this thing does not work in a loop. I guess this is my prob. There might be something wrong with the boolean. I tryed it with different settings, but still wrong: Here is what I mean: $fruit = array('apple','banana','cranberry'); $findme = array('apple', 'notlisted'); foreach($findme AS $value){ if (($key = array_search($value, $fruit)) !== false) { print "Key ($key) was found from value $value<br>"; } else { print "Sorry, $value was not found in array \$fruit<br>"; } } Replys: Key (0) was found from value apple Key () was found from value notlisted So there must be still something wron in th stmt. Thanx for your help, Andy "Philip Olson" <philip@cornado.com> schrieb im Newsbeitrag news:Pine.BSF.4.10.10204251646060.3971-100000@localhost... > If the key is 0, array_search will return 0, it does not > start at 1. > > $arr = array('apple','banana','cranberry'); > $key = array_search('apple', $arr); > > print $key; // 0 > > If 'apple' was not found, $key would then equal > to boolean false. Be sure to use "=== false" > to check failure because 0 == false. For example: > > $fruit = array('apple','banana','cranberry'); > $findme = 'apple'; > > if (($key = array_search($findme, $fruit)) !== false) { > print "Key ($key) was found from value $findme"; > } else { > print "Sorry, $findme was not found in array \$fruit"; > } > > Again, remember, 0 == false. == !=, === !==. So, 0 !== false. > Wow that sounds confusing. :) Also consider the sexy array_keys() > function. > > Regards, > Philip Olson > > p.s. > http://uk.php.net/manual/en/language.operators.comparison.php > p.s.s. also take into account extra whitespace (trim), and potential > issues with case sensitivity (strtolower). > > > On Thu, 25 Apr 2002, andy wrote: > > > Hi there, > > > > I am passing an array through the URL with a ',' inbetween: > > var=php,mysql,super > > Parsing is done with: explode (',',$var). This gives me an array starting > > with 0 > > > > Later on I have to search for lets say php with array_search. > > > > Unfortunatelly array_search requires an array starting with 1. So php is not > > found. > > > > Does anybody know a workaround for this? > > > > Thanx, > > > > Andy > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > >

« previous php.general (#94597) next »