Home  >  Article  >  Backend Development  >  Does php string appear in array

Does php string appear in array

PHPz
PHPzOriginal
2023-05-05 18:53:46582browse

Whether PHP strings appear in arrays

In PHP, strings and arrays are common data types. It often involves searching for a string in an array, which can be achieved using PHP's built-in functions. This article will introduce several commonly used methods to check whether a string appears in an array.

Method 1: in_array() function

The in_array() function is a built-in array function in PHP, used to check whether the specified value appears in the array. This function takes two parameters: the value to find and the array to search.

For example:

$names = array('Tony', 'John', 'David');

if (in_array('Tony', $names)) {
    echo "Tony is in the array.";
} else {
    echo "Tony is not in the array.";
}

In the above code, we create an array $names containing three elements and use the in_array() function to find the string 'Tony' in the array. Since 'Tony' appears in the array, the output result is:

Tony is in the array.

If the string we are looking for is not in the array, the output result is:

Tony is not in the array.

Although in_array() The function is very simple and easy to use, but it is just a Boolean function that can only tell us whether the element we are looking for appears in the array, and cannot return its position in the array.

Method 2: array_search() function

The array_search() function is also a built-in array function in PHP, used to search for a specified value in an array and return its corresponding key name.

For example:

$names = array('Tony', 'John', 'David');
$key = array_search('Tony', $names);

if ($key !== false) {
    echo "Tony is in the array, its key is $key.";
} else {
    echo "Tony is not in the array.";
}

In the above code, we use the array_search() function to find the string 'Tony' in the array. Since 'Tony' appears in the array and its position in the array is determined to be 0, the output result is:

Tony is in the array, its key is 0.

It should be noted that if the element to be found is not in the array, array_search() The function will return false. Therefore, we use the !== operator to determine whether the return value is false.

Method 3: Using a foreach loop

Another way to find occurrences of a string in an array is to use a foreach loop to traverse the array and use the string comparison function strcmp() to compare the array elements Is it equal to the string to be found? If equal, return the position information, otherwise continue the loop.

For example:

$names = array('Tony', 'John', 'David');

foreach ($names as $key => $value) {
    if (strcmp($value, 'Tony') == 0) {
        echo "Tony is in the array, its key is $key.";
        break;
    }
}

if ($key == count($names)) {
    echo "Tony is not in the array.";
}

In the above code, we use a foreach loop to traverse the array $names, and use the strcmp() function to compare whether each array element and the string 'Tony' are equal. If they are equal, print their position information and use the break statement to jump out of the loop.

It should be noted that if the string you are looking for is not in the array, you need to use the $count() function to compare whether the loop variable $key and the number of array elements are equal to determine whether the character has been searched. string.

Conclusion

This article introduces three common methods in PHP that can be used to check whether a string appears in an array. The in_array() function is suitable for simple search operations, the array_search() function allows position information to be returned while searching, and the use of a foreach loop allows the use of custom comparison functions. In practical applications, we can choose appropriate methods to perform search operations according to different needs.

The above is the detailed content of Does php string appear in array. 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