Home > Backend Development > PHP Problem > How to determine whether a value belongs to an array in php

How to determine whether a value belongs to an array in php

PHPz
Release: 2023-04-18 15:30:45
Original
463 people have browsed it

In PHP, there are several ways to determine whether a value belongs to an array. This article will introduce these methods and give sample code.

  1. in_array() function

in_array() function can check whether a value is in the array and returns true if it is, otherwise it returns false. The syntax of this function is as follows:

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

Where $needle is the value to be found, $haystack is the array, $strict is an optional parameter, if set to true, type checking will be performed during comparison. Here is an example:

$my_array = array("apple", "banana", "orange");
if (in_array("apple", $my_array)) {
    echo "apple is in the array";
} else {
    echo "apple is not in the array";
}
Copy after login
  1. array_search() function

array_search() function searches for a value in an array and returns its key if not found Return false. The syntax of this function is as follows:

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

Where $needle is the value to be found, $haystack is the array, $strict is an optional parameter, if set to true, type checking will be performed during comparison. Here is an example:

$my_array = array("apple", "banana", "orange");
$key = array_search("banana", $my_array);
if ($key !== false) {
    echo "banana is at index $key";
} else {
    echo "banana is not in the array";
}
Copy after login
Copy after login
  1. isset() function

isset() function can check whether a value exists and returns true if it exists, otherwise it returns false. When determining whether a value is in an array, you can use the value as the key of the array. Here is an example:

$my_array = array("apple" => 1, "banana" => 2, "orange" => 3);
if (isset($my_array["apple"])) {
    echo "apple is in the array";
} else {
    echo "apple is not in the array";
}
Copy after login
  1. array_key_exists() function

array_key_exists() function can check whether a key exists in the array and returns true if it exists, otherwise it returns false. The following is an example:

$my_array = array("apple" => 1, "banana" => 2, "orange" => 3);
if (array_key_exists("apple", $my_array)) {
    echo "apple is a key in the array";
} else {
    echo "apple is not a key in the array";
}
Copy after login
  1. The difference between in_array() and array_search() functions

Although both in_array() and array_search() functions can check whether a value is in array, but their return values ​​are different. The in_array() function returns true or false, while the array_search() function may return a numeric value or false. For example, the following code will output "banana is at index 1":

$my_array = array("apple", "banana", "orange");
$key = array_search("banana", $my_array);
if ($key !== false) {
    echo "banana is at index $key";
} else {
    echo "banana is not in the array";
}
Copy after login
Copy after login
  1. Using a foreach loop

The last way to check whether a value is in the array is Use a foreach loop to iterate through each value in the array and compare them to see if they are equal to the value you are looking for. Here is an example:

$my_array = array("apple", "banana", "orange");
$found = false;
foreach ($my_array as $value) {
    if ($value == "banana") {
        $found = true;
        break;
    }
}
if ($found) {
    echo "banana is in the array";
} else {
    echo "banana is not in the array";
}
Copy after login

The above are several ways to determine whether a value belongs to an array in PHP. Using these methods makes it easier for us to work with arrays.

The above is the detailed content of How to determine whether a value belongs to 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