Home  >  Article  >  Backend Development  >  How to determine if a word is in an array using PHP

How to determine if a word is in an array using PHP

PHPz
PHPzOriginal
2023-04-12 09:22:50439browse

During the development process using PHP, we often encounter situations where we need to determine whether a certain word is in an array. Today we will introduce in detail how to use PHP to determine whether a word is in an array.

The answer is actually very simple, just use the in_array() function. The function of in_array() function is to determine whether a value exists in the array. The following is the syntax of the in_array() function:

in_array($value, $array, $strict);

Among them, $value represents the value that needs to be judged, $array represents the array that needs to be judged, and $strict represents whether to use strict mode (optional, the default value is false ).

Next, we will use an example to demonstrate how to use the in_array() function to determine whether a word is in an array:

<?php

$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);

if (in_array(&#39;apple&#39;, $arr)) {
    echo &#39;apple is in the array&#39;;
} else {
    echo &#39;apple is not in the array&#39;;
}

?>

In the above code, we created an array containing three elements array, and then use the in_array() function to determine whether "value" exists in the array. Since "value" is an element in the array, the if statement will return true and the program will output "apple is in the array".

In addition to using the in_array() function, we can also use another function in PHP - array_search() to determine whether a word is in an array. The function of array_search() function is to find the specified value in the array and return its subscript. If the lookup fails, false is returned. The following is the syntax of the array_search() function:

array_search($value, $array, $strict);

Among them, $value represents the value that needs to be found in the array, $array represents the array that needs to be queried, and $strict represents whether to use strict mode (optional, default value is false).

Next, we will use an example to demonstrate how to use the array_search() function to determine whether a word is in an array:

<?php

$arr = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);

$key = array_search(&#39;apple&#39;, $arr);

if ($key !== false) {
    echo &#39;apple is in the array, its key is &#39; . $key;
} else {
    echo &#39;apple is not in the array&#39;;
}

?>

In the above code, we created an array containing three elements array, and then use the array_search() function to find "value" in the array. Since "value" is an element in the array, the array_search() function returns the subscript where the element is located, so the if statement will return true and the program will output "apple is in the array, its key is 0".

It should be noted that if you need to determine whether the value in the array is Boolean false, you need to use the in_array() function instead of the array_search() function. Because the array_search() function treats Boolean false and the number 0 the same, this may lead to errors in judgment.

Through the above two methods, we can easily determine whether a word is in the array. I hope this article can be helpful to everyone.

The above is the detailed content of How to determine if a word is in an array using PHP. 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