Home > Backend Development > PHP Problem > 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

王林
Release: 2023-03-05 15:32:02
Original
4157 people have browsed it

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)
Copy after login

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 "匹配未找到";
  }
?>
Copy after login

Output:

匹配已找到
Copy after login

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!

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