Home > php教程 > php手册 > body text

浅谈PHP检查数组中是否存在某个值 in_array 函数,浅谈in_array

WBOY
Release: 2016-07-06 14:24:43
Original
777 people have browsed it

浅谈PHP检查数组中是否存在某个值 in_array 函数,浅谈in_array

PHP in_array() 函数检查数组中是否存在某个值,如果存在则返回 TRUE ,否则返回 FALSE 。

语法:

bool in_array( mixed needle, array array [, bool strict] )
Copy after login

参数说明:

参数 说明
needle 需要在数组中搜索的值,如果是字符串,则区分大小写
array 需要检索的数组
strict 可选,如果设置为 TRUE ,则还会对 needle 与 array 中的值类型进行检查

例子:

<&#63;php
$arr_a = array("a", "b", "c", 1);
if(in_array("a", $arr_a)){
  echo '字符 a 在 $arr_a 数组中存在';
} else {
  echo '字符 a 在 $arr_a 数组中不存在';
}
&#63;>
Copy after login

例子输出结果如下:

字符 a 在 $arr_a 数组中存在严格检查的例子:

<&#63;php
$arr_a = array("a", "b", "c", 1);
if(in_array("1", $arr_a, TRUE)){
  echo '字符 1 在 $arr_a 数组中存在';
} else {
  echo '字符 1 在 $arr_a 数组中不存在';
}
&#63;>
Copy after login

例子输出结果如下:

字符 1 在 $arr_a 数组中不存在数组作为 needle 的例子:

<&#63;php
$arr_a = array(array("a", "b"), 1, 2);
$arr_b = array("a", "b");
if(in_array($arr_b, $arr_a)){
	echo '数组 $arr_b 在 $arr_a 数组中存在';
} else {
	echo '数组 $arr_b 在 $arr_a 数组中不存在';
}
&#63;>
Copy after login

例子输出结果如下:

数组 $arr_b 在 $arr_a 数组中存在

以上这篇浅谈PHP检查数组中是否存在某个值 in_array 函数就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!