Re: how to make array_search start from 0?
| From: | Philip Olson | Date: | Thu, 25 Apr 2002 17:06:42 +0000 |
| Subject: | Re: how to make array_search start from 0? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-94586@lists.php.net to get a copy of this message | ||
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
>