Home  >  Article  >  Backend Development  >  PHP determines whether an element appears in an array

PHP determines whether an element appears in an array

小云云
小云云Original
2018-02-26 13:18:1620397browse

In PHP, you can use the in_array() function to directly determine whether an element is in an array. If the element exists in the array, the in_array() function returns true, otherwise it returns false. This article mainly shares with you how to use PHP to determine whether an element appears in an array. I hope it can help you.


Syntax

in_array(search,array,type)
##ParametersDescriptionRequired. Specifies the value to search for in the array. Required. Specifies the array to search. 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.

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

Since PHP 4.2, the search parameter may now also be an array

For example:

  1. $arr=array("107","Website","Studio");

  2. ##if(in_array("107",$arr)){

  3. echo "Matching successful";

  4. }else{

  5.    echo "Match failed";

  6. }

The running result is:

Matching successfully


##

  1. ##$arr=array("107" ,"Website","Studio");

  2. ##if(in_array("Henan University",$arr )){

  3. echo "Matching successful" ;

  4. }

    else{

  5. echo "Match failed";

  6. }

    ##The running result is:
Failed to match

search
array
type

The above is the detailed content of PHP determines whether an element appears 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