21xrx.com
2024-04-26 19:49:50 Friday
登录
文章检索 我的文章 写文章
PHP in_array()函数
2021-08-29 09:05:11 深夜i     --     --
PHP IN_ARRAY()函数

 

在数组中搜索值“glenn”并输出一些文本:

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn", $people))
  {
  echo "Match found";
  }
else
  {
  echo "Match not found";
  }
?>

定义和用法

in_array()函数搜索特定值的数组。

注意:如果搜索参数是字符串,则类型参数设置为true,搜索区分大小写。


句法

in_array(search, array, type)

参数值

参数 描述
search 必需的。 指定要搜索的内容
array 必需的。 指定要搜索的数组
type 可选的。 如果此参数设置为true,则in_array()函数在数组中搜索字符串和特定类型。

 


技术细节

返回值: 如果在数组中找到不到值返回false,否则返回true
PHP版本: 4+
PHP Changelog: PHP 4.2:搜索参数现在可以是数组

更多例子

使用所有参数:

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);

if (in_array("23", $people, TRUE))
  {
  echo "Match found<br>";
  }
else
  {
  echo "Match not found<br>";
  }
if (in_array("Glenn",$people, TRUE))
  {
  echo "Match found<br>";
  }
else
  {
  echo "Match not found<br>";
  }

if (in_array(23,$people, TRUE))
  {
  echo "Match found<br>";
  }
else
  {
  echo "Match not found<br>";
  }
?>

 

 

  
  
下一篇: PHP key()函数

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章