php - 查找数组是否包含元素

Mat*_*hew 19 php arrays function

我有一个只有一个id列表的数组,如下所示:

$my_array = array(
 12, 17, 99, 23
);
Run Code Online (Sandbox Code Playgroud)

现在我知道我可以做以下事情:

function in_array($haystack = array(), $needle = NULL)
{
 foreach($haystack as $id)
 {
  if ($id == $needle)
  {return TRUE;}
  else
  {return FALSE;}
 }
}
Run Code Online (Sandbox Code Playgroud)

但似乎可能已经建立了一个功能.我能用什么?

Sar*_*raz 24

不需要创建一个,它已经与您正在使用的名称同时出现:in_array也是如此.

例:

if (in_array('foo', $array)){
  // foo is in the array
}
Run Code Online (Sandbox Code Playgroud)


Jos*_*eld 5

它被称为in_array() xD