How to determine whether an array contains a certain value in php

PHPz
Release: 2023-04-17 14:56:40
Original
1721 people have browsed it

In PHP, there are many ways to determine whether an array contains a certain value. This article will introduce several of them.

Method 1: Use the in_array() function

The in_array() function is a function in PHP used to check whether a certain value exists in an array. Its syntax is as follows:

in_array($value, $array);
Copy after login

Among them, $value is the value to be found, and $array is the array to be found. Returns true if the value is found in the array, false otherwise.

Sample code:

<?php
$fruits = array("apple", "banana", "orange", "pear");
if (in_array("banana", $fruits)) {
    echo "找到了!";
}
else {
    echo "没有找到!";
}
?>
Copy after login

Output result:

找到了!
Copy after login
Copy after login
Copy after login

Method 2: Use array_search() function

array_search() function is also used in arrays Find a specific value. Its syntax is as follows:

array_search($value, $array);
Copy after login

Among them, $value is the value to be found, and $array is the array to be found. If the value is found, its key name is returned, otherwise false is returned.

Sample code:

<?php
$fruits = array("apple", "banana", "orange", "pear");
$key = array_search("banana", $fruits);
if ($key !== false) {
    echo "找到了,键名为:" . $key;
}
else {
    echo "没有找到!";
}
?>
Copy after login

Output result:

找到了,键名为:1
Copy after login

Method 3: Use isset() function and key name

If you don’t care about the specific value, If you just want to determine whether a certain key exists in the array, you can also use the isset() function. If the key exists, returns true, otherwise returns false.

Sample code:

<?php
$fruits = array("apple" => 1, "banana" => 2, "orange" => 3, "pear" => 4);
if (isset($fruits["banana"])) {
    echo "找到了!";
}
else {
    echo "没有找到!";
}
?>
Copy after login

Output result:

找到了!
Copy after login
Copy after login
Copy after login

Method 4: Use the array_key_exists() function

Similar to the isset() function, array_key_exists() Functions can also be used to check whether a key exists in an array. If the key exists, returns true, otherwise returns false.

Sample code:

<?php
$fruits = array("apple" => 1, "banana" => 2, "orange" => 3, "pear" => 4);
if (array_key_exists("banana", $fruits)) {
    echo "找到了!";
}
else {
    echo "没有找到!";
}
?>
Copy after login

Output result:

找到了!
Copy after login
Copy after login
Copy after login

To sum up, there are many ways to determine whether an array contains a certain value in PHP. When using it, you can choose the appropriate method according to actual needs.

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

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