Re: "searching" arrays
| From: | Richard Lynch | Date: | Wed, 15 Aug 2001 05:26:56 +0000 |
| Subject: | Re: "searching" arrays | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-62770@lists.php.net to get a copy of this message | ||
> is there also a way to search for, let's say '*china*' because in my array
> it would say "Republic of China" and when searching for "china" I get no
> result from the array...
You'd have to walk through the array and use strstr or ereg for that.
You could maybe use PHP's array_walk function, but that might be a bit
tricky for a beginner, unless you've programmed before...
function my_array_search($needle, $haystack){
# Go ahead and use the built-in one, in case it's a perfect match:
$result = array_search($needle, $haystack);
reset($haystack);
while (!($result === FALSE) && (list($k, $v) = each($haystack)){
# Or maybe you wanted ereg($needle, $v) here...
$result = ereg($v, $needle) ? $k : FALSE;
}
return $result;
}
--
WARNING richard@zend.com address is an endangered species -- Use
ceo@l-i-e.com
Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm