Home > Backend Development > PHP Problem > How to determine whether a string exists in an array in php

How to determine whether a string exists in an array in php

PHPz
Release: 2023-04-20 09:55:23
Original
553 people have browsed it

PHP is a very popular programming language that is widely used in web development. In many web development projects, we need to determine whether a string is in an array. This article will introduce how to determine whether a string exists in an array in PHP.

Specifically, we can use the in_array() function to determine whether a string exists in the array. The basic syntax of the in_array() function is as follows:

in_array($needle, $haystack);
Copy after login

Among them, $needle represents the string to be found, and $haystack represents the array to be found. If $needle exists in $haystack, then the in_array() function will return true; otherwise, it will return false.

Let’s take a look at some specific examples.

First example:

<?php
$colors = array(&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;);
if (in_array(&#39;green&#39;, $colors)) {
    echo "green exists in the array!";
} else {
    echo "green does not exist in the array.";
}
?>
Copy after login

In this code, we define a $colors array, and then use the in_array() function to determine whether the string 'green' exists in the array. Since 'green' does exist in the array, this code prints "green exists in the array!".

Second example:

<?php
$months = array(&#39;January&#39;, &#39;February&#39;, &#39;March&#39;, &#39;April&#39;, &#39;May&#39;);
$month_to_find = &#39;June&#39;;
if (in_array($month_to_find, $months)) {
    echo "$month_to_find exists in the array!";
} else {
    echo "$month_to_find does not exist in the array.";
}
?>
Copy after login

In this code, we define a $months array, and then set $month_to_find to 'June'. Since the string 'June' does not exist in the $months array, the code will output "$month_to_find does not exist in the array.".

The third example:

<?php
$fruits = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
$fruit_to_find = &#39;Apple&#39;;
if (in_array(strtolower($fruit_to_find), array_map(&#39;strtolower&#39;, $fruits))) {
    echo "$fruit_to_find exists in the array!";
} else {
    echo "$fruit_to_find does not exist in the array.";
}
?>
Copy after login

In this code, we define a $fruits array and then set $fruit_to_find to 'Apple'. Note that the string we want to judge here is not case-sensitive. In order to achieve this function, we need to convert all strings in $fruit_to_find and $fruits arrays to lowercase letters before calling the in_array() function. Specifically, we can use the strtolower() function to convert a string to lowercase.

In this example, we use the array_map() function to iterate through the $fruits array and convert the strings in it to lowercase letters. When calling the in_array() function, we can convert $fruit_to_find and all $fruits strings to lowercase letters to achieve case-insensitive comparisons.

To summarize, judging whether a string exists in an array is a very basic operation and a very common operation in web development. PHP's built-in in_array() function can easily accomplish this task. At the same time, we can also achieve case-ignoring comparison by converting strings to lowercase letters. Hope this article can be helpful to you!

The above is the detailed content of How to determine whether a string exists in an array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template