Re: case-insensitive in_array()

From: Date: Thu, 23 Nov 2000 06:08:50 +0000
Subject: Re: case-insensitive in_array()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-26793@lists.php.net to get a copy of this message
On Wed, 22 Nov 2000, Matt McClanahan wrote: > On Thu, 23 Nov 2000, Gerald Kaszuba wrote: > > > Hi.. > > > > Current in_array() function: > > bool in_array (mixed needle, array haystack) > > > > I want one that does this: > > bool in_arrayi(string needle, array haystack) > > > > Don't tell me I need to do it the long way :P > > What's so bad about the long way? > > function in_arrayi($needle, $haystack) > { > $keys = array_keys($haystack); > foreach ($keys as $key) > { > if (strtolower($key) == strtolower($needle)) > return true; > } > return false; > } Right, I'll be taking a moment to get drunk now. I appoligize, you didn't ask to find a key, in_array() searches an array's values. :P You could remove the array_keys step and switch around the foreach, but it wouldn't recurse. But, as I look at in_array, I'm not sure that recurses either. So I guess it's fine. Here's another version: function in_arrayi($needle, $haystack) { foreach ($haystack as $value) { if (strtolower($value) == strtolower($needle)) return true; } return false; } I took a shot at making one recurse, but it only went down one level. So I leave that as an exercise for somebody who knows more than I do. Matt

« previous php.general (#26793) next »