in_array() and multidimensional array

前端 未结 22 1743
眼角桃花
眼角桃花 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:22

    Great function, but it didnt work for me until i added the if($found) { break; } to the elseif

    function in_array_r($needle, $haystack) {
        $found = false;
        foreach ($haystack as $item) {
        if ($item === $needle) { 
                $found = true; 
                break; 
            } elseif (is_array($item)) {
                $found = in_array_r($needle, $item); 
                if($found) { 
                    break; 
                } 
            }    
        }
        return $found;
    }
    

提交回复
热议问题