Home >Backend Development >PHP Tutorial >A brief discussion on PHP checking whether a certain value exists in an array in_array function, a brief discussion on in_array_PHP tutorial

A brief discussion on PHP checking whether a certain value exists in an array in_array function, a brief discussion on in_array_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:49:42670browse

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1136646.htmlTechArticleA brief talk about PHP checking whether a certain value exists in the array in_array function, a brief talk about in_array PHP in_array() function checking array Whether a certain value exists in, if so, returns TRUE, otherwise returns...
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