Home  >  Article  >  Backend Development  >  How to determine if php is in an array or not

How to determine if php is in an array or not

PHPz
PHPzOriginal
2023-04-26 14:21:08480browse

In PHP, determining whether a value is in an array is a common operation. In order to achieve this goal, we need to learn various array-related functions and operators, such as in_array() function, array_search() function, in_array() operator and array_key_exists() function, etc. In this article, we will detail the use of these methods to determine whether a value is within an array in PHP.

  1. in_array() function

in_array() function in PHP is a function used to check whether a value is in an array. Its syntax is as follows:

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

Among them, $needle represents the value to be searched, $haystack represents the array to be searched, and $strict represents whether strict mode is enabled.

Strict mode will compare the data type and value, and will return true only if the data type and value are exactly the same. When strict mode is enabled, the in_array() function will not treat the numeric string "123" and the integer 123 as equal.

In the in_array() function, the function will return true if the value is found, otherwise it will return false.

The following is an example of using the in_array() function to determine whether the value is in the array:

$fruits = array("apple", "banana", "pineapple", "orange");

if (in_array("banana", $fruits)) {
    echo "找到了!";
} else {
    echo "没找到...";
}

In the above code, the $fruits array contains four kinds of fruits, we use the in_array() function to check if there is "banana" in it. Since "banana" exists in $fruits, the above code will print "Found!".

  1. array_search() function

array_search() function can be used to find the key name of a specific value in an array. Its syntax is as follows:

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

Relative to the in_array() function, the array_search() function returns not true or false, but the key name of a specific value in the array. If not found, the function returns false.

Like the in_array() function, the array_search() function can also enable strict mode, but when using the array_search() function, you need to compare the return value with false instead of using an if statement to determine whether the return result is Is true or false.

The following is an example of using the array_search() function to determine whether the value is in the array:

$fruits = array("apple", "banana", "pineapple", "orange");

$search_result = array_search("pineapple", $fruits);

if ($search_result != false) {
    echo "找到了!键名为:" . $search_result;
} else {
    echo "没找到...";
}

In the above code, we use the array_search() function to find "pineapple" in the $fruits array key name within. Since "pineapple" is the third element of $fruits, the key is 2. Therefore, the above code will output "Found! Key name: 2".

  1. in_array() operator

In PHP, there is another way to use an operator to find whether a value is within an array, which is the in_array() operator. Similar to the in_array() function, the in_array() operator is used to check whether a value is in an array and returns true when the value is found, otherwise it returns false.

The following is an example of using the in_array() operator to determine whether the value is in the array:

$fruits = array("apple", "banana", "pineapple", "orange");

if ("banana" in $fruits) {
    echo "找到了!";
} else {
    echo "没找到...";
}

This code will output "Found!" because "banana" exists in the $fruits array middle.

  1. array_key_exists() function

array_key_exists() function can be used to check whether the specified key name exists in the array. Its syntax is as follows:

bool array_key_exists ( mixed $key , array $array )

Among them, $key represents the key name to be searched, and $array represents the array to be searched.

Like the in_array() function, the array_key_exists() function will return true or false, but the array_key_exists() function only checks the key name, not the key value.

The following is an example of using the array_key_exists() function to determine whether the key name is in the array:

$fruits = array("apple" => "苹果", "banana" => "香蕉", "pineapple" => "菠萝", "orange" => "橘子");

if (array_key_exists("banana", $fruits)) {
    echo "找到了!中文名为:" . $fruits["banana"];
} else {
    echo "没找到...";
}

In the above code, the $fruits array contains four key-value pairs, each key name It represents the English name of a fruit, and the key value represents the Chinese name of the fruit. We use the array_key_exists() function to check if the key "banana" exists in $fruits. Since "banana" exists in $fruits, the output is "Found! Chinese name: banana".

Summary

In PHP, to determine whether a value is in an array, you can use the in_array() function, array_search() function, in_array() operator, array_key_exists() function, etc. method. Each method has its unique advantages and limitations, and we need to choose the method that best suits us based on the actual situation to achieve judgment.

The above is the detailed content of How to determine if php is in an array or not. 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