Home > php教程 > 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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 14:24:43
Original
859 people have browsed it

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] )
Copy after login

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;>
Copy after login

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;>
Copy after login

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;>
Copy after login

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.

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template