How to determine if a value is in an array in php

藏色散人
Release: 2023-03-09 08:56:01
Original
2894 people have browsed it

In PHP, you can use the in_array function to determine whether a value is in an array. The function of in_array is to search whether a specified value exists in the array. The syntax of this function is "in_array(search,array,type) ”, where the parameter search specifies the value to be searched in the array.

How to determine if a value is in an array in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

in_array() function searches whether the specified array exists value.

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

Syntax

in_array(search,array,type)
Copy after login

Parameters

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.

Description

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.

Return value: TRUE if the value is found in the array, FALSE otherwise.

PHP Version: 4

Changelog: As of PHP 4.2, the search parameter may now also be an array.

【Recommended learning: PHP video tutorial

Example

Use all parameters:

<?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>";
  }
?>
Copy after login

Running result:

匹配未找到
匹配已找到
匹配未找到
Copy after login

The above is the detailed content of How to determine if a value is in an array in php. For more information, please follow other related articles on the PHP Chinese website!

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