in_array() and multidimensional array

前端 未结 22 1659
眼角桃花
眼角桃花 2020-11-22 00:30

I use in_array() to check whether a value exists in an array like below,

$a = array(\"Mac\", \"NT\", \"Irix\", \"Linux\");
if (in_array(\"Irix\"         


        
22条回答
  •  春和景丽
    2020-11-22 01:17

    Here is my proposition based on json_encode() solution with :

    • case insensitive option
    • returning the count instead of true
    • anywhere in arrays (keys and values)

    If word not found, it still returns 0 equal to false.

    function in_array_count($needle, $haystack, $caseSensitive = true) {
        if(!$caseSensitive) {
            return substr_count(strtoupper(json_encode($haystack)), strtoupper($needle));
        }
        return substr_count(json_encode($haystack), $needle);
    }
    

    Hope it helps.

提交回复
热议问题