Home >Backend Development >PHP Problem >How to check if a string is in an array using php

How to check if a string is in an array using php

PHPz
PHPzOriginal
2023-03-28 15:00:351584browse

In PHP development, querying whether a string is in an array is a very common operation. In this article, we will explore some methods to check if a string is in a PHP array.

Method 1: Use the in_array() function

The in_array() function in PHP is used to check whether a value exists in an array. We can use this function to find whether a string is in an array:

$array = array('apple', 'banana', 'orange', 'grape');

if (in_array('banana', $array)) {
    echo 'Found';
} else {
    echo 'Not Found';
}

This code will output: Found. Because 'banana' exists in the array.

Let's change it to search for another string 'watermelon':

$array = array('apple', 'banana', 'orange', 'grape');

if (in_array('watermelon', $array)) {
    echo 'Found';
} else {
    echo 'Not Found';
}

This code will output: Not Found, because 'watermelon' is not in the array.

Method 2: Using the array_search() function

Now, let’s look at another way to find whether a string is in a PHP array. Use the array_search() function:

$array = array('apple', 'banana', 'orange', 'grape');

if (array_search('banana', $array)) {
    echo 'Found';
} else {
    echo 'Not Found';
}

This code will output: Found. Because 'banana' exists in the array.

Let's change it to search for another string 'watermelon':

$array = array('apple', 'banana', 'orange', 'grape');

if (array_search('watermelon', $array)) {
    echo 'Found';
} else {
    echo 'Not Found';
}

This code will output: Not Found, because 'watermelon' is not in the array.

Method 3: Use in_array() and strtolower() functions

In the above two methods, we did not consider the case issue. So the following method ignores case:

$array = array('Apple', 'Banana', 'Orange', 'Grape');

$search_term = 'banana';

if (in_array(strtolower($search_term), array_map('strtolower', $array))) {
    echo 'Found';
} else {
    echo 'Not Found';
}

Use the array_map() function to process the string traversing the entire array, use the strtolower() function to convert the string to lowercase and store it in a new array middle. We can then use the in_array() function to find whether the specified string is in the new array.

If we instead search for another string such as 'Watermelon':

$array = array('Apple', 'Banana', 'Orange', 'Grape');

$search_term = 'Watermelon';

if (in_array(strtolower($search_term), array_map('strtolower', $array))) {
    echo 'Found';
} else {
    echo 'Not Found';
}

This outputs Not Found because there is no string in the new array that matches 'Watermelon'.

At this point, we have mastered three different methods in PHP to check whether a string exists in an array. From a performance perspective, the in_array() function and array_search() function seem to be more efficient and do not involve traversing the entire array elements.

The above is the detailed content of How to check if a string 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