in_array() and multidimensional array

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

    This will do it:

    foreach($b as $value)
    {
        if(in_array("Irix", $value, true))
        {
            echo "Got Irix";
        }
    }
    

    in_array only operates on a one dimensional array, so you need to loop over each sub array and run in_array on each.

    As others have noted, this will only for for a 2-dimensional array. If you have more nested arrays, a recursive version would be better. See the other answers for examples of that.

提交回复
热议问题