...

View Full Version : case insensitive in array



zodehala
05-23-2008, 03:28 PM
$letter = array
(
1 => A,
2 => B,
3 => C,
4 => D,

);

echo array_search("a", $letter);

it cannot find lowercase a why ?

derzok
05-23-2008, 03:52 PM
This was in the user comments at http://us2.php.net/array_search



<?php
function array_isearch($str, $array) {
foreach($array as $k => $v) {
if(strcasecmp($str, $v) == 0) return $k;
}
return false;
}
?>


What I usually do is:


foreach($array as $x) {
if(strcmp(strtoupper($x),strtoupper($key)) {
return $x;
}
}


Which is about the same thing except the first example takes care of case with strcasecmp while I take care of it with strtoupper. Theirs is probably faster.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum