Home > Backend Development > PHP Tutorial > in_array() Finds whether an array value exists Case sharing

in_array() Finds whether an array value exists Case sharing

php中世界最好的语言
Release: 2023-03-27 06:54:02
Original
1670 people have browsed it

This time I bring you in_array() to find out whether an array value exists. What are the precautions for in_array() to find out whether an array value exists? The following is a practical case. , let’s take a look.

In phpProgramming, the in_array() function searches for a given value in an array.

in_array() Definition and Usage
The in_array() function searches an array for a given value.

Syntax

in_array(value,array,type)
Copy after login

Parameter Description
value Required. Specifies the value to search for in the array.
array required. Specifies the array to search.
type Optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same.

Description
Returns true if the given value value exists in the array array. If the third parameter is set to true, the function returns true only if the element exists in the array and the data type is the same as the given value.

If the parameter is not found in the array, the function returns false.

Note: If the value parameter is a string, and the type parameter is set to true, the search is case-sensitive.

Example 1, in_array instance.

<?php $people = array("Peter", "Joe", "Glenn", "Cleveland");
if (in_array("Glenn",$people)) { echo "Match found"; } else { echo "Match not found"; } ?>
Copy after login

Output:
Match found

Example 2, in_array instance.

<?php $people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
if (in_array("23",$people, TRUE)
)
{ 
echo "Match found<br />"; 
} 
else 
{ 
echo "Match not found<br />"; 
}if (in_array("Glenn",$people, TRUE)) 
{ 
echo "Match found<br />"; 
} 
else 
{ 
echo "Match not found<br />"; 
}if (in_array(23,$people, TRUE)) 
{ 
echo "Match found<br />"; 
} 
else 
{ 
echo "Match not found<br />"; 
} 
?>
输出: 
Match not found 
Match found 
Match found
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Yii framework configuration controller and action steps detailed explanation

array_key_exists() function detailed explanation of the array key name search steps

The above is the detailed content of in_array() Finds whether an array value exists Case sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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