in_array() and multidimensional array

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

    You could always serialize your multi-dimensional array and do a strpos:

    $arr = array(array("Mac", "NT"), array("Irix", "Linux"));
    
    $in_arr = (bool)strpos(serialize($arr),'s:4:"Irix";');
    
    if($in_arr){
        echo "Got Irix!";
    }
    

    Various docs for things I used:

    • strpos()
    • serialize()
    • Type Juggling or (bool)

提交回复
热议问题