List Info

Thread: note 71445 added to function.in-array




note 71445 added to function.in-array
user name
2006-11-29 13:08:12
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.co
m.
----
Server IP: 66.163.161.117
Probable Submitter: 83.104.121.66 (proxied: 1.1 BANDEXCH)
----
Manual Page -- ht
tp://www.php.net/manual/en/function.in-array.php
Edit        -- https://master
.php.net/note/edit/71445
Del: integrated  -- h
ttps://master.php.net/note/delete/71445/integrated
Del: useless     -- http
s://master.php.net/note/delete/71445/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/71445/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/71445
Reject      -- https://mast
er.php.net/note/reject/71445
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )