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

青灯夜游
Release: 2023-03-15 08:56:01
Original
2608 people have browsed it

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

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 "没有找到匹配项!";
}
?>
Copy after login

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

  • 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 "没有找到匹配项!";
}
?>
Copy after login

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!

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