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

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 in_array() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 476 ::
收藏到网摘: 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

登陆 | 还没注册?