Home  >  Article  >  Backend Development  >  Explore how PHP determines whether a certain string exists in an array

Explore how PHP determines whether a certain string exists in an array

PHPz
PHPzOriginal
2023-04-25 09:07:06625browse

In php, we often need to determine whether an array contains a certain string. In this article, we will explore how PHP determines whether a certain string exists in an array, and how to find and filter elements in an array.

Method 1: Use the in_array() function

php provides the in_array() function, which can easily find whether the specified element is included in the array. The syntax of this function is as follows:

in_array($search, $array);

The $search parameter is the element to be found, and the $array parameter is the array to be found. Returns true if the $search element is contained in $array, false otherwise. The sample code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi');
$search1 = 'banana';
$search2 = 'grape';

if (in_array($search1, $fruits)) {
    echo "Fruits array contains $search1";
} else {
    echo "Fruits array does not contain $search1";
}

if (in_array($search2, $fruits)) {
    echo "Fruits array contains $search2";
} else {
    echo "Fruits array does not contain $search2";
}

Execute the above code, the result will be:

Fruits array contains banana
Fruits array does not contain grape

Method 2: Use the array_search() function

php also provides the array_search() function , this function can find the first occurrence of an element in the array. The syntax of this function is as follows:

array_search($search, $array);

The $search parameter is the element to be found, and the $array parameter is the array to be found. If the search element is contained in the array, returns the key name of the first occurrence of the element in the array; otherwise, returns false. The sample code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi');
$search1 = 'banana';
$search2 = 'grape';

$result1 = array_search($search1, $fruits);
$result2 = array_search($search2, $fruits);

if ($result1 !== false) {
    echo "$search1 found at position $result1";
} else {
    echo "$search1 not found in the array";
}

if ($result2 !== false) {
    echo "$search2 found at position $result2";
} else {
    echo "$search2 not found in the array";
}

Execute the above code, the result will be:

banana found at position 1
grape not found in the array

Method 3: Use in_array() combined with array_map() function

Sometimes, we You need to find whether the array contains any element in a set of strings. At this time, we can use the in_array() function combined with the array_map() function. The array_map() function executes a user-defined function on each element of the array and returns a new array containing the results of all function processing. We can call the in_array() function in the array_map() function to find the value. The sample code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi');
$search = array('apple', 'watermelon');

$found = array_filter(array_map(function($needle) use($fruits){
    return in_array($needle, $fruits);
}, $search));

if ($found) {
    echo "Found these fruits: ";
    print_r($found);
} else {
    echo "No fruits found";
}

Execute the above code, the result will be:

Found these fruits: Array
(
    [0] => 1
)

In the above code, we use the array_map() function and array_filter() function. The array_map() function iterates through each element in the $search array, calling an anonymous function on each element that checks whether the element is contained in the $fruits array. The array_filter() function receives an array and returns an array containing only elements that are true. In this case, $found contains only those positions where the element exists.

Method 4: Use the preg_grep() function

If we need to find elements in the array that contain a specific pattern, such as all elements starting with the letter 'a', we can use preg_grep in php ()function. The preg_grep() function returns an array containing all elements matching the pattern. The sample code is as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi');
$pattern = '/^a/';

$found = preg_grep($pattern, $fruits);

if ($found) {
    echo "Found these fruits: ";
    print_r($found);
} else {
    echo "No fruits found";
}

Execute the above code, the result will be:

Found these fruits: Array
(
    [0] => apple
)

In the above code, we use the regular expression "/^a/" to match letters Elements starting with 'a'. The preg_grep() function receives a regular expression and the array to be searched, and it returns an array containing all elements that match the expression.

Summary

This article introduces four different methods in PHP to find whether an array contains a certain string. These methods include using the in_array() function, array_search() function, in_array() function combined with array_map() function, and preg_grep() function. These methods are all commonly used array query methods in PHP. At work, we can choose the appropriate method to apply according to the specific situation.

The above is the detailed content of Explore how PHP determines whether a certain string exists in an 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