Heim  >  Artikel  >  php教程  >  浅谈PHP检查数组中是否存在某个值 in_array 函数,浅谈in_array

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

WBOY
WBOYOriginal
2016-07-06 14:24:43777Durchsuche

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

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

语法:

bool in_array( mixed needle, array array [, bool strict] )

参数说明:

参数 说明
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;>

例子输出结果如下:

字符 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;>

例子输出结果如下:

字符 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;>

例子输出结果如下:

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

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn