Home  >  Article  >  Backend Development  >  PHP determines whether the array contains

PHP determines whether the array contains

WBOY
WBOYOriginal
2023-05-19 22:58:42966browse

PHP is a server-side scripting language widely used in web development. Like other programming languages, PHP also has many commonly used functions and operations, such as determining whether an array contains an element. In PHP, we can use the following ways to achieve this function.

1. in_array() function

The in_array() function can determine whether a value exists in the array. If it exists, it returns true, otherwise it returns false.

Syntax: in_array($value, $array);

where $value represents the value to be found, and $array represents the array to be found.

For example, we have an array containing some products. If we want to determine whether a certain product exists, we can use the in_array() function:

$goods = array("Apple"," Banana","Orange","Pear");
if(in_array("Banana",$goods)){

echo "该商品已存在";

}

If the product "Banana" exists in the array In $goods, "This product already exists" will be output.

2. array_search() function

The array_search() function can search for the key (key) of a value in the array. If it exists, it returns the corresponding key, otherwise it returns false.

Syntax: array_search($value, $array);

where $value represents the value to be found, and $array represents the array to be searched.

For example, we have an array containing some student information. If we want to find the name of a student, we can use the array_search() function:

$students = array("001"= >"Tom","002"=>"John","003"=>"Mary","004"=>"Joe");
$key = array_search("Mary",$ students);
if($key){

echo "该学生的学号为:".$key;

}

If the student "Mary" exists in the array $students, it will output "The student number is: 003 ".

3. Enhanced version of in_array()

If we need to determine whether a value exists in the array and is not case-sensitive, we can use the following code:

function in_array_case($value,$array){

return in_array(strtolower($value),array_map('strtolower',$array));

}

Here we use an anonymous function array_map() to convert all elements in the array to lowercase letters, so that even if the array Even if the capitalization in the elements is different, you can also correctly determine whether an element is included.

For example, we have an array containing some city names. If we want to find a certain city, ignoring case, we can use the in_array_case() function:

$cities = array("Beijing ","Shanghai","Guangzhou","Shenzhen");
if(in_array_case("shanghai",$cities)){

echo "该城市已存在";

}

If city "Shanghai" Exists in the array $cities, "The city already exists" will be output.

The above are several methods for PHP to determine whether an array contains an element. You can choose different methods according to specific needs. In actual development, choosing the appropriate method can make the code more concise and efficient.

The above is the detailed content of PHP determines whether the array contains. 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