How to determine whether it is included in an array in php

PHPz
Release: 2023-04-23 19:44:40
Original
328 people have browsed it

In PHP, it is a common operation to determine whether an element (numeric value or string) is in an array. This process is achieved by using the in_array() function, which is very simple and efficient to use.

The in_array() function has two required parameters and one optional parameter. The first parameter is the target element, the second parameter is the array containing the target element, and the third parameter is a Boolean value used to check whether strict type comparison needs to be used.

The following is an example of a simple check using the in_array() function, which will check if 'apple' is in the following array:

$fruits = array('banana', 'orange', 'apple');

if (in_array('apple', $fruits)) {
    echo "'apple' 被找到了!";
} else {
    echo "'apple' 没有被找到 :(";
}
Copy after login

When running this code, the final output will be "'apple' found!".

In this example, the in_array() function checks whether the array $fruits contains the element 'apple'. Since 'apple' exists in the array $fruits, the conditional branch will output "'apple' was found!". It's very simple!

By default, the in_array() function performs case-insensitive comparisons on strings. So if we define the array as follows:

$fruits = array('banana', 'orange', 'APPLE');
Copy after login

The code will still print "'apple' found!". The reason is that if the second argument (i.e. the array) contains string elements (which is not case sensitive), in_array() will still match the elements.

If you wish to use case-sensitive comparison, you must pass the Boolean value TRUE in the third parameter of the function, as follows:

$fruits = array('banana', 'orange', 'APPLE');

if (in_array('apple', $fruits, TRUE)) {
    echo "'apple' 被找到了!";
} else {
    echo "'apple' 没有被找到 :(";
}
Copy after login

The output will be "'apple' None Found:(" because we are now performing a case-sensitive comparison. 'APPLE' is not equal to 'apple'.

Alternatively, you can perform more advanced searches using the array_search() function. array_search() function Returns the first key in the array that matches the target element. If not found, returns FALSE.

Here is an example of using the array_search() function to check if 'apple' is in the array:

$fruits = array('banana', 'orange', 'apple');
$key = array_search('apple', $fruits);

if ($key !== false) {
    echo "'apple' 被找到了! 键是 $key";
} else {
    echo "'apple' 没有被找到 :(";
}
Copy after login

The output will be "'apple' found! Key is 2". The reason is:

  • The string 'apple' was found in the $fruits array.
  • The array_search() function returns the key matching 'apple', which is 2.

In actual programming, you may need to search complex multi-dimensional arrays or perform advanced filters to make your judgment logic more Powerful. However, using the in_array() and array_search() functions in PHP is always a good starting point.

The above is the detailed content of How to determine whether it is included in an array in php. 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!