in_array() and multidimensional array

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

    if your array like this

    $array = array(
                  array("name" => "Robert", "Age" => "22", "Place" => "TN"), 
                  array("name" => "Henry", "Age" => "21", "Place" => "TVL")
             );
    

    Use this

    function in_multiarray($elem, $array,$field)
    {
        $top = sizeof($array) - 1;
        $bottom = 0;
        while($bottom <= $top)
        {
            if($array[$bottom][$field] == $elem)
                return true;
            else 
                if(is_array($array[$bottom][$field]))
                    if(in_multiarray($elem, ($array[$bottom][$field])))
                        return true;
    
            $bottom++;
        }        
        return false;
    }
    

    example : echo in_multiarray("22", $array,"Age");

提交回复
热议问题