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

PHPz
Release: 2023-04-19 14:28:27
Original
715 people have browsed it

In PHP programming, array is a very common data type that can store multiple values, and these values ​​can be of different data types. In actual development, we often need to perform various operations on arrays, including determining whether a value is in the array.

Let’s discuss how to determine whether a value exists in an array in PHP.

  1. in_array function

The in_array() function is a function built into PHP to determine whether a value is in an array. Its syntax is as follows:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Copy after login

Among them, $needle represents the value to be judged, $haystack represents the array to be judged, and $strict represents whether to use strict mode for comparison (the default is false, that is, strict mode is not used).

The following is an example of a simple in_array() function:

$a = array('apple', 'banana', 'orange');
if (in_array('banana', $a)) {
    echo "存在";
} else {
    echo "不存在";
}
Copy after login

The output result is: "Exists".

  1. array_search function

The array_search() function is similar to the in_array() function. It is also a function used to determine whether a value is in an array. The difference is that array_search() The function will return the key name (i.e. subscript) of the found value in the array, or false if it is not found.

The syntax is as follows:

mixed array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Copy after login

The following is an example of a simple array_search() function:

$a = array('apple', 'banana', 'orange');
$key = array_search('banana', $a);
if ($key !== false) {
    echo "存在,键名为:$key";
} else {
    echo "不存在";
}
Copy after login

The output result is: "Exists, key name: 1".

  1. isset function

isset() function is used to determine whether a variable exists, and can also be used to determine whether a value is in an array. The syntax is as follows:

bool isset ( mixed $var [, mixed $... ] )
Copy after login

Among them, $var represents the variable or value to be judged, and $... represents multiple optional variables or values.

The following is an example of a simple isset() function:

$a = array('apple', 'banana', 'orange');
if (isset($a[1])) {
    echo "存在";
} else {
    echo "不存在";
}
Copy after login

The output result is: "Exists".

  1. array_key_exists function

array_key_exists() function is a function used to determine whether the specified key name (i.e., subscript) exists in the array. Its syntax is as follows:

bool array_key_exists ( mixed $key , array $array )
Copy after login

Among them, $key represents the key name to be judged, and $array represents the array to be judged.

The following is an example of a simple array_key_exists() function:

$a = array('a' => 'apple', 'b' => 'banana', 'c' => 'orange');
if (array_key_exists('b', $a)) {
    echo "存在";
} else {
    echo "不存在";
}
Copy after login

The output result is: "Exists".

Summary

The above is the method for determining whether a certain value exists in an array in PHP, including the in_array() function, array_search() function, isset() function and array_key_exists() function. In actual development, we can choose the appropriate method to make judgments according to needs.

The above is the detailed content of How to determine whether a certain value exists in an array 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!