How to use in_array function in PHP to find whether a specified value exists in an array

王林
Release: 2023-06-26 16:02:02
Original
1588 people have browsed it

In PHP development, you often need to find whether an array contains a certain value. At this time, you can use the in_array() function provided by PHP to achieve this. This article will introduce how to use the in_array() function to find whether a specified value exists in an array.

The in_array() function is a PHP built-in function used to determine whether a specified value exists in the array. Its syntax is as follows:

bool in_array(mixed $value, array $array [, bool $strict = FALSE ])
Copy after login

This function accepts three parameters, where $value indicates the value to be found, $array indicates the array to be found, and $strict indicates whether strict mode is enabled. If $strict is set to TRUE, it is required that the elements in $value and $array must have equal types in addition to equal values.

When the function is executed successfully, the return value is TRUE, otherwise the return value is FALSE.

The following is a sample code that uses the in_array() function to find whether a specified value exists in an array:

// 定义一个数组
$arr = array('apple', 'banana', 'orange', 'grape');

// 判断数组中是否存在指定值
if (in_array('apple', $arr)) {
    echo '存在'; // 输出:存在
} else {
    echo '不存在';
}
Copy after login

In the above sample code, we first define an array $arr for judging Whether the specified value exists in the array. Then, we use the in_array() function to find whether the value 'apple' exists in the array. Since the value 'apple' does exist in the array, the in_array() function returns TRUE, and we can output the string 'exists'.

In addition, we can also turn on strict mode by setting the third parameter $strict, which requires that elements not only have equal values, but also have equal types. The sample code is as follows:

// 定义一个数组
$arr = array(1, '2', 3);

// 判断数组中是否存在指定值
if (in_array('2', $arr, true)) {
    echo '存在';
} else {
    echo '不存在'; // 输出:不存在
}
Copy after login

In the above sample code, we pass an array $arr containing integer, string and floating point types as a parameter to the in_array() function and set $strict The parameter is TRUE. Then, we use the in_array() function to find whether there is a string value of '2' in the array.

Since the $strict parameter is set to TRUE, the in_array() function compares the types of elements, so it returns FALSE. Because in the $arr array, the type of '2' is string, and what we are looking for is character type '2'.

To sum up, the in_array() function is a very useful function in PHP development and is often used to find whether an array contains a certain value. When using this function, you need to pay attention to the setting of the $strict parameter to avoid unexpected results.

The above is the detailed content of How to use in_array function in PHP to find whether a specified value exists in an array. For more information, please follow other related articles on the PHP Chinese website!

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 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!