How to determine if a value exists in an array in php? Brief analysis of methods

PHPz
Release: 2023-04-14 17:47:13
Original
829 people have browsed it

In PHP development, we often need to determine whether a certain value exists in an array in order to perform related operations as needed. Next, this article will use practical examples to introduce the related usage of PHP to determine whether a certain value exists in the array.

1. Use the in_array function to determine

In PHP, you can use the in_array function to determine whether a value exists in an array. The specific usage is as follows:

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

Among them, the $needle parameter is the value to be searched, the $haystack parameter is the array to be searched, and the $strict parameter is whether to perform type checking. If the $strict parameter is true, check whether the type of $needle is consistent with the type of the value in $haystack. If they are inconsistent, return false.

The following is a sample code using the in_array function to make a judgment:

$arr = array('apple', 'banana', 'orange', 'pear');
if (in_array('orange', $arr)) {
    echo 'orange is in the array';
} else {
    echo 'orange is not in the array';
}
Copy after login

The output result is: orange is in the array, indicating that the value orange exists in the $arr array.

Of course, the $needle parameter can also be an array, so that you can determine whether multiple values ​​exist in the $haystack array at one time. The sample code is as follows:

$arr = array('apple', 'banana', 'orange', 'pear');
$search_arr = array('banana', 'grape');
$res_arr = array_intersect($search_arr, $arr);
if ($res_arr) {
    echo implode(',', $res_arr) . ' are in the array';
} else {
    echo 'no value in the array';
}
Copy after login
Copy after login

The output result is: banana is in the array, indicating that the value banana exists in the $arr array.

2. Use array key value to determine

In addition to using the in_array function, you can also use the array key value to determine whether a value exists in an array. The specific method is to first use the array_keys function to obtain all keys in the array, and then use the in_array function to determine whether the value exists in the key. The sample code is as follows:

$arr = array('apple', 'banana', 'orange', 'pear');
if (in_array('orange', array_keys($arr))) {
    echo 'orange is in the array';
} else {
    echo 'orange is not in the array';
}
Copy after login

Similarly, you can also determine whether multiple values ​​exist in the array at one time. The sample code is as follows:

$arr = array('apple', 'banana', 'orange', 'pear');
$search_arr = array('banana', 'grape');
$keys_arr = array_keys($arr);
$res_arr = array_intersect($search_arr, $keys_arr);
if ($res_arr) {
    echo implode(',', $res_arr) . ' are in the array';
} else {
    echo 'no value in the array';
}
Copy after login

3. Use the in_array function for fuzzy matching

If you want to perform fuzzy matching, you can also use the in_array function. The specific method is to compare each value in the $haystack array with the $needle parameter, and return true if there is a value with the same prefix as $needle. The sample code is as follows:

$arr = array('apple', 'banana', 'orange', 'pear');
$prefix = 'or';
foreach ($arr as $k => $v) {
    if (strpos($v, $prefix) === 0) {
        echo $v . "\n";
    }
}
Copy after login

The output result is: orange.

4. Use the array_intersect function for judgment

In addition to using the in_array function and array key value judgment, you can also use the array_intersect function for judgment. The specific usage is to put the value to be found into an array, and then use the array_intersect function to take out the intersection of the two arrays and determine whether the intersection array has a value. The sample code is as follows:

$arr = array('apple', 'banana', 'orange', 'pear');
$search_arr = array('banana', 'grape');
$res_arr = array_intersect($search_arr, $arr);
if ($res_arr) {
    echo implode(',', $res_arr) . ' are in the array';
} else {
    echo 'no value in the array';
}
Copy after login
Copy after login

The output result is: banana is in the array.

Summary

This article introduces a variety of methods to determine whether a value exists in an array in php, including using the in_array function, using array key value judgment, using the in_array function for fuzzy matching, and using array_intersect function to judge. Different methods are suitable for different scenarios, and developers can choose according to actual needs.

The above is the detailed content of How to determine if a value exists in an array in php? Brief analysis of methods. 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!