note 71445 added to function.in-array

From: Date: Wed, 29 Nov 2006 13:08:12 +0000
Subject: note 71445 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-120159@lists.php.net to get a copy of this message
In trying to emulate a case insensitive in_array(), I came up with ... <?php function in_array_caseless($m_Needle, array $a_Haystack, $b_Strict = False) { // Let's assume the function will fail. $b_Result = False; // Compare array vs array if (is_array($m_Needle)) { // Iterate the Haystack to compare each Bale with the Needle. foreach($a_Haystack as $a_Bale) { // Determine the intersection between the Needle and the Bale. // Strictness indicates that the associative indexes must also match using case insensitivity. if ( ($b_Strict && (array_uintersect_uassoc($m_Needle, $a_Bale, 'strcasecmp', 'strcasecmp') === $m_Needle)) || (!$b_Strict && (array_uintersect($m_Needle, $a_Bale, 'strcasecmp') === $m_Needle)) ) { // It it matches then we have a winner. $b_Result = True; // Don't process anything else as 1 match is all that is required. break; } } } // Compare everything else but reverse the Haystack and the Needle. // This is done as the return from array_uinterect is based upon its Needle // and we need the Haystack's type to compare for strictness. else { $a_Result = array_values(array_uintersect($a_Haystack, array($m_Needle), 'strcasecmp')); $b_Result = !$b_Strict || (gettype($m_Needle) == gettype($a_Result[0])); } // Return the result return $b_Result; } ?> For more info on this and other cool functions please see http://rquadling.php1h.com. ---- Server IP: 66.163.161.117 Probable Submitter: 83.104.121.66 (proxied: 1.1 BANDEXCH) ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- https://master.php.net/note/edit/71445 Del: integrated -- https://master.php.net/note/delete/71445/integrated Del: useless -- https://master.php.net/note/delete/71445/useless Del: bad code -- https://master.php.net/note/delete/71445/bad+code Del: spam -- https://master.php.net/note/delete/71445/spam Del: non-english -- https://master.php.net/note/delete/71445/non-english Del: in docs -- https://master.php.net/note/delete/71445/in+docs Del: other reasons-- https://master.php.net/note/delete/71445 Reject -- https://master.php.net/note/reject/71445 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120159) next »