PHP function introduction—in_array(): Check whether a specific element exists in the array

王林
Release: 2023-07-25 11:08:01
Original
670 people have browsed it

PHP function introduction—in_array(): Check whether a specific element exists in the array

In PHP, there are many built-in functions that can be used to process and operate arrays. One of the very useful functions is the in_array() function. The purpose of this function is to check whether a specific value exists in the given array. It can help us quickly determine whether an element exists in the array, thereby facilitating related operations.

Syntax:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Parameter description:

  • $needle : The value to be found can be any type of element.
  • $haystack: Array to be found.
  • $strict: Optional parameter, if set to TRUE, the in_array() function will compare data types.

Return value:
If found, return TRUE; otherwise, return FALSE.

Below we use some sample code to demonstrate the use of the in_array() function.

Example 1: Basic Usage

$fruits = array("apple", "banana", "orange", "grape");
if (in_array("banana", $fruits)) {
    echo "找到了banana!";
} else {
    echo "没有找到banana!";
}
Copy after login

Output:
Found banana!

Explanation: In the above example, we defined a container containing four fruits Array $fruits. We use the in_array() function to check if there is an element with the value "banana" in the array. Since the element does exist in the array, "Found banana!" is output.

Example 2: Using strict mode

$numbers = array(1, 2, "3", 4, 5);
if (in_array("3", $numbers, true)) {
    echo "找到了3!";
} else {
    echo "没有找到3!";
}
Copy after login

Output:
3 not found!

Explanation: In the above example, we defined a string containing numbers and characters Array of strings $numbers. We use strict mode to compare elements, which requires both the value and type of the element to be equal to the target value. Since "3" in the array is a string, not a numeric type, in strict mode, the in_array() function does not find a matching element. So the output is "3 not found!".

Example 3: Find in a multidimensional array

$people = array(
    array("name" => "John", "age" => 20),
    array("name" => "Mary", "age" => 30),
    array("name" => "David", "age" => 25)
);
if (in_array(array("name" => "Mary", "age" => 30), $people)) {
    echo "找到了Mary!";
} else {
    echo "没有找到Mary!";
}
Copy after login

Output:
Mary found!

Explanation: In the above example, we defined a A two-dimensional array of associative arrays $people. We use the in_array() function to check whether there is an element that exactly matches the specified associative array. Since the element does exist in the array, "Mary found!" is output.

Summary:
The in_array() function is a very practical function in PHP. It can help us quickly check whether a specific element exists in a given array. This is very convenient when working with arrays. When using this function, we can choose whether to enable strict mode to compare elements. In addition, we can also search in multi-dimensional arrays.

In daily PHP development, we often need to determine whether an element exists in an array. The in_array() function can help us quickly solve this problem. I hope the above example can help you better understand and use the in_array() function.

The above is the detailed content of PHP function introduction—in_array(): Check whether a specific element exists in the array. For more information, please follow other related articles on the PHP Chinese website!

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