Home  >  Article  >  Backend Development  >  How to determine whether a specified element is in an array in php

How to determine whether a specified element is in an array in php

王林
王林Original
2020-09-01 13:33:054108browse

php method to determine whether the specified element is in the array: You can use the [in_array()] function to determine. If the element exists in the array, the function returns true, otherwise it returns false, such as [in_array() "mack",$arr)].

How to determine whether a specified element is in an array in php

php method to determine whether a specified element is in an array:

In PHP we can use in_array() The function directly determines whether an element is in the array. If the element exists in the array, the in_array() function returns true, otherwise it returns false.

(Recommended tutorial: php video tutorial)

Function syntax:

in_array(search,array,type)

Parameter introduction:

  • search 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.

Explanation:

Returns true if the given value search 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 has the same data type as the given value.

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

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

(Related tutorial recommendations: php training)

Example:

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

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

Output:

匹配已找到

The above is the detailed content of How to determine whether a specified element is in an array in php. 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