当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > in_array() 函数

PHP
PHP 介绍
PHP 安装
PHP 语法
PHP 变量
PHP操作符
PHP If...Else
PHP Switch
PHP 数组
PHP 循环
PHP 函数
PHP 表单
PHP $_GET
PHP $_POST
PHP Date
PHP Include
PHP 文件处理
PHP 文件上传
PHP Cookies
PHP Sessions
PHP 发送邮件

PHP 中的 in_array() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 475 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The in_array() function searches an array for a specific value.
in_array()函数的作用是:在数组中搜索指定的值。

This function returns TRUE if the value is found in the array, or FALSE otherwise.
如果这个值存在于数组中,该函数将返回True,否则将返回False。

Syntax
语法

in_array(search,array,type)

Parameter参数 Description描述
search Required. Specifies the what to search for
必要参数。指定需要搜索的值
array Required. Specifies the array to search
必要参数。指定在哪个数组中进行搜索
type Optional. If this parameter is set, the in_array() function searches for the search-string and specific type in the array
可选参数。如果设置这个参数,那么in_array()函数将搜索数组中指定要搜索的字符串和详细的类型


Tips and Notes
注意点

Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
注意:如果搜索(search)参数是一个字符串,并且type参数设置为TRUE,那么整个搜索将是区分大小写的。


Example 1
案例1

<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn",$people)) { echo "Match found"; }
else { echo "Match not found"; }
?>

The output of the code above will be:
上述代码将输出下面的结果:

Match found


Example 2
案例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 />"; }
?>

The output of the code above will be:
上述代码将输出下面的结果:

Match not found
Match found
Match not found

评论 (0) All

登陆 | 还没注册?