Welcome! 閮餃
蝢撖園 蝢撖嗥曄 蝢撖嗉憯 蝢撖嗉賣 蝢撖嗅啣

Advanced

Change History

Message: [PHP] Case-insensitive in_array() funciton (without foreach)

Changed By: mepoadm
Change Date: June 20, 2012 03:34PM

[PHP] Case-insensitive in_array() funciton (without foreach)
For a case-insensitive in_array(), you can use array_map() to avoid a foreach statement, e.g.:

<pre>&lt;?php
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
?>
</pre>

SReference and source:
1. <a href=http://us2.php.net/manual/en/function.in-array.php#89256>User Contributed Notes in PHP Manuel</a> (2009.02)
2. <a href=http://stackoverflow.com/questions/2166512/php-case-insensitive-in-array-function>Q&A Discussion in Stack Overflow</a> (2010.10)

Original Message

雿: mepoadm
Date: June 20, 2012 03:29PM

[PHP] Case-insensitive in_array() funciton (without foreach)
For a case-insensitive in_array(), you can use array_map() to avoid a foreach statement, e.g.:

<?php
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
?>


Source:
User Contributed Notes in PHP Manuel (2009.02)