How to use PHP in_array with associative array?

前端 未结 6 1698
夕颜
夕颜 2020-12-03 00:15

Is there any php function such as in_array for associative arrays you get by the mysql function \"mysql_fetch assoc\" ?

For example, if I have an $array that looks l

6条回答
  •  天涯浪人
    2020-12-03 00:45

    You can't do it directly on nested arrays.. You need to nest it down a bit and then do it.

    array('ID'=>1, 'name'=>"Smith"), 1=>array('ID'=>2, 'name'=>"John"));
    
    foreach($arr as $arr1)
    {
        if(in_array(1,$arr1))
        {
           echo "Yes found.. and the correspoding key is ".key($arr1)." and the employee is ".$arr1['name'];
        }
    }
    

    OUTPUT :

    Yes found.. and the correspoding key is ID and the employee is Smith
    

提交回复
热议问题