PHP - 发现如果一个数组包含一个元素 [英] php - find if an array contains an element

查看:113
本文介绍了PHP - 发现如果一个数组包含一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组只是一个ID列表,像这样:

I have an array with just a list of ids, like so:

$my_array = array(
 12, 17, 99, 23
);

现在我知道我大概可以做这样的事情:

Now I know I could probably do something like:

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

但似乎已经有可能建成一个功能。我能有什么用?

but it seems like there's probably already a function built. What could I use?

推荐答案

没有必要创建一个,它已经存在的合作与捎带你正在使用相同的名称: in_array 了。

No need to create one, it is already there co-incidentally with the same name you are using: in_array too.

示例:

if (in_array('foo', $array)){
  // foo is in the array
}

这篇关于PHP - 发现如果一个数组包含一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆