Home  >  Article  >  Backend Development  >  php in_array function usage (example)

php in_array function usage (example)

王林
王林Original
2020-08-05 11:53:523401browse

php in_array function usage (example)

Function introduction:

in_array() function is used to search whether the specified value exists in the array. Returns TRUE if the value is found in the array, FALSE otherwise.

(Recommended tutorial: php graphic tutorial)

Function syntax:

bool in_array(mixed $needle, array $haystack[, bool $strict = FALSE])

Parameter introduction:

  • needle Required. Specifies the value to search for in the array.​

  • haystack Required. Specifies the array to search.

  • #strict Optional. If this parameter is set to TRUE, the in_array() function checks whether the data being searched is of the same type as the value of the array.

Code example one:

Search for the value "phpcn" in the array and output some text:

<?php
$sites = array("Google", "phpcn", "Taobao", "Facebook"); 
if (in_array("phpcn", $sites)){
    echo "找到匹配项!";
}else{
    echo "没有找到匹配项!";
}
?>

Output result:

找到匹配项!

(Learning video recommendation: php video tutorial)

Code example two:

<?php
$people = array("Bill", "Steve", "Mark", "David");

if (in_array("23", $people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
if (in_array("Mark",$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }

if (in_array(23,$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
?>

Output result:

匹配未找到
匹配已找到
匹配未找到

The above is the detailed content of php in_array function usage (example). 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