Home > Backend Development > PHP Problem > php exists in the array

php exists in the array

WBOY
Release: 2023-05-06 14:06:07
Original
520 people have browsed it

PHP is a widely used server-side scripting language commonly used for web development. During the development process, we often need to operate on arrays, such as finding whether a certain value exists in the array. So, in PHP, how to determine whether a value exists in an array?

First, we can use the in_array() function to determine whether a value exists in an array. This function requires two parameters, the first parameter is the value to be searched, and the second parameter is the array to be searched. If the search is successful, the function returns true, otherwise it returns false.

The following is an example:

$fruit = array("apple", "banana", "orange");
if (in_array("banana", $fruit)) {
    echo "banana exists in the array";
} else {
    echo "banana does not exist in the array";
}
Copy after login

In the above code, we define a fruit array $fruit, and then use the in_array() function to find whether the value "banana" exists. Since the $fruit array contains "banana", the output result is "banana exists in the array".

In addition to the in_array() function, we can also use the array_search() function to find the key of the value in the array. This function also takes two parameters, the first parameter is the value to be found, and the second parameter is the array to be found. If the search is successful, this function returns the key corresponding to the value in the array, otherwise it returns false.

The following is an example:

$fruit = array("apple", "banana", "orange");
$key = array_search("banana", $fruit);
if ($key !== false) {
    echo "banana exists in the array, its key is " . $key;
} else {
    echo "banana does not exist in the array";
}
Copy after login

In the above code, we use the array_search() function to find the key corresponding to the value "banana" in the array. Since the $fruit array contains "banana", the output result is "banana exists in the array, its key is 1".

It should be noted that if you want to determine whether a value exists in a multi-dimensional array, the above two methods are not suitable. At this point, we can use a recursive function to achieve this. The following is an example:

function in_multiarray($value, $array) {
    foreach ($array as $item) {
        if (is_array($item) && in_multiarray($value, $item)) {
            return true;
        } else if ($item == $value) {
            return true;
        }
    }
    return false;
}

$fruit = array("apple", "banana", array("orange", "grape"));
if (in_multiarray("grape", $fruit)) {
    echo "grape exists in the multi-dimensional array";
} else {
    echo "grape does not exist in the multi-dimensional array";
}
Copy after login

In the above code, we define a recursive function in_multiarray(), which is used to determine whether a value exists in a multidimensional array. If it exists, return true, otherwise return false. In this example, we define a fruit array $fruit, which in turn contains an array to store certain fruits. We use the in_multiarray() function to find whether the value "grape" exists. Since the $fruit array contains "grape", the output result is "grape exists in the multi-dimensional array".

In summary, to determine whether a value exists in an array in PHP, we can use the in_array() function or array_search() function. If we need to determine whether a value exists in a multi-dimensional array, we can use a recursive function. The use of these functions is very common in web development, and it is very important for developers to master them.

The above is the detailed content of php exists in the 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