search
HomeBackend DevelopmentPHP ProblemHow to check whether a certain value in an array exists in php

How to query whether it exists: 1. Use the in_array() function, the syntax "in_array(value, $array)"; 2. Use the array_search() function, the syntax "array_search(value, $array)" ".

How to check whether a certain value in an array exists in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php query array Whether a value exists

Method 1: Use the in_array() function

The in_array() function can find whether the array contains a certain value , returns TRUE if it exists, and returns FALSE if it does not exist. The syntax format is as follows:

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

The parameter description is as follows:

  • $needle: is the value to be searched. If $needle is a string, the comparison is case-sensitive. ;
  • $array: is the array to be searched;
  • $strict: is an optional parameter, the default is FALSE.
    • If $strict is empty or FALSE, the in_array() function will only check whether the value of $needle is equal to the value in $array;
    • If the value of $strict is TRUE, in_array In addition to checking the values ​​in $needle and $array, the () function also compares their types for equality.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$sites = array("Google", "Taobao", "Facebook");
if (in_array("PHP中文网", $sites))
{
    echo "找到匹配项!";
}
else
{
    echo "没有找到匹配项!";
}
?>

How to check whether a certain value in an array exists in php

##Method 2: Use array_search() function

array_search() function searches for a certain key value in the array. If the specified key value is found in the array, it returns the corresponding key name, otherwise it returns FALSE. The syntax format is as follows:

array_search(value,array,strict)

  • value Required. Specifies the key value to search for in the array.

  • array Required. Specifies the array to be searched.

  • strict Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:

    • true

    • false - Default

    If set to true, then Checking the type of the given value in the array, the number 5 and the string 5 are different (see Example 2).

If a key value is found more than once in the array, the key name matching the first found key value is returned.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$sites = array("Google", "Taobao", "Facebook");
if (array_search("Taobao", $sites))
{
    echo "找到匹配项!";
}
else
{
    echo "没有找到匹配项!";
}
?>

How to check whether a certain value in an array exists in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to check whether a certain value in an array exists 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool