Home >Backend Development >PHP Problem >PHP determines whether data exists in array

PHP determines whether data exists in array

PHPz
PHPzOriginal
2023-05-19 16:32:09698browse

In php, it is often necessary to determine whether a piece of data exists in an array. At this time, you can use the following methods:

1. Use the in_array function:

The in_array function is a function commonly used in PHP to determine whether a data exists in an array. Its function prototype is :

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

Among them, the needle parameter indicates the value to be found, haystack indicates the array to be found, and strict indicates whether to use strict mode for comparison. For example:

$arr = array(1, 2, 3, 'a', 'b', 'c');
if (in_array(1, $arr)) {
    echo '存在';
} else {
    echo '不存在';
}

In the above code, first define an array $arr, and then use the in_array function to determine whether 1 is in it. Since 1 is indeed in the array, the output "exists".

2. Use the array_search function:

The array_search function is another function that determines whether data exists in an array. Its function prototype is:

mixed array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Among them, the needle parameter Indicates the value to be found, haystack indicates the array to be searched, and strict indicates whether to use strict mode for comparison. If found, the function returns the key name of the element in the array, otherwise it returns false. For example:

$arr = array(1, 2, 3, 'a', 'b', 'c');
$key = array_search('a', $arr);
if ($key !== false) {
    echo '存在于数组中,键名为:' . $key;
} else {
    echo '不存在';
} 

In the above code, define an array $arr, and then use the array_search function to find the value 'a' in the array. If it exists, return the key name, otherwise return false.

3. Use the custom functions of in_array and array_search:

Sometimes we need to customize the function to determine whether the data exists in the array according to different needs. For example, if we want to determine whether a string is a substring of another string (that is, whether it contains), we can use the following code:

function is_substr($str, $substr) {
    return strpos($str, $substr) !== false;
}

$str = 'hello world';
$substr = 'world';
if (is_substr($str, $substr)) {
    echo '包含';
} else {
    echo '不包含';
}

In the above code, a custom function is_substr is defined to determine a character Whether the string is a substring of another string, the built-in function strpos of PHP is used. This function returns the position of a string within another string, or false if it does not exist. If it is found, that is, the position is non-negative, the function returns true, otherwise it returns false.

To sum up, in PHP to determine whether data exists in an array, the in_array and array_search functions are the most commonly used and convenient. If you have special needs, you can customize the corresponding functions.

The above is the detailed content of PHP determines whether data exists in array. For more information, please follow other related articles on the PHP Chinese website!

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