Home  >  Article  >  php教程  >  A brief discussion on PHP's in_array function to check whether a certain value exists in an array, a brief discussion on in_array

A brief discussion on PHP's in_array function to check whether a certain value exists in an array, a brief discussion on in_array

WBOY
WBOYOriginal
2016-07-06 14:24:43816browse

A brief talk about the in_array function in PHP to check whether a certain value exists in the array, a brief talk about in_array

PHP in_array() function checks whether a certain value exists in the array, and returns if it exists TRUE, otherwise returns FALSE.

Syntax:

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

Parameter description:

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

Example:

<&#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;>

The example output results are as follows:

Example of strict check for character a in $arr_a array:

<&#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;>

The example output results are as follows:

Character 1 does not exist in the $arr_a array. Example of array as 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;>

The example output results are as follows:

Array $arr_b exists in array $arr_a

The above article briefly talks about PHP checking whether a certain value exists in the array. The in_array function is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia.

Statement:
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