Home > Backend Development > PHP Problem > How to determine if there is a certain value in an array in php

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

PHPz
Release: 2023-04-26 11:19:58
Original
730 people have browsed it

PHP is a popular server-side scripting language that supports a variety of commonly used data structures and algorithms. Arrays are a common data structure used to store a set of key-value pairs.

When dealing with arrays, we usually need to check whether the array contains a specific value. PHP provides a variety of methods to determine whether a certain value exists in an array. This article will introduce several common methods.

  1. in_array function

The in_array function in PHP can be used to check whether a value exists in an array. Its function is defined as follows:

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

This function The parameter description is as follows:

  • needle: The value to be checked.
  • haystack: The array to search for.
  • strict: Optional parameter. If strict mode is turned on, the data type and value must be equal to return true. Default is false.

The following is an example of using the in_array function to determine whether a certain value exists in the array:

$arr = array('apple', 'banana', 'orange');
if (in_array('apple', $arr)) {
    echo '数组中包含apple';
} else {
    echo '数组中不包含apple';
}
Copy after login

In the above code, the in_array function first receives the value 'apple' to be checked and The array to find $arr as argument. The function returns a boolean value, true if 'apple' exists in $arr, false otherwise. If the array contains 'apple', then output "the array contains apple".

  1. array_search function

array_search function is used to find a given value in an array and return its key. Its function is defined as follows:

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

The The parameter description of the function is as follows:

  • needle: The value to be found.
  • haystack: The array to search for.
  • strict: Optional parameter. If strict mode is turned on, the data type and value must be equal to return true. Default is false.

The following is an example of using the array_search function to determine whether a certain value exists in the array:

$arr = array('apple', 'banana', 'orange');
$key = array_search('apple', $arr);
if ($key !== false) {
    echo '数组中包含apple,其键为'.$key;
} else {
    echo '数组中不包含apple';
}
Copy after login

In the above code, the array_search function first receives the value 'apple' to be found and The array to find $arr as argument. The function returns the key of the value in the array, or false if the value does not exist. If the array contains 'apple', then output "The array contains apple and its key is 0".

It should be noted that the array_search function returns a key value, not a Boolean value, so you need to use "!==false" to check whether the value exists.

  1. isset function

In PHP, you can also use the isset function to determine whether a certain value exists in the array. The function is defined as follows:

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

The parameter description of this function is as follows:

  • var: The variable or array element to be checked.
  • ...: Optional other variables or array elements.

The following is an example of using the isset function to determine whether a certain value exists in an array:

$arr = array('apple', 'banana', 'orange');
if (isset($arr[0])) {
    echo '数组中包含'.$arr[0];
} else {
    echo '数组中不包含'.$arr[0];
}
Copy after login

In the above code, the isset function receives the variable or array element to be checked $arr [0] as parameter. Returns true if the value exists, false otherwise. If the array contains 'apple', then output "the array contains apple".

It should be noted that the isset function can only check whether the variable or array element exists, but cannot check whether its value is null or false.

To sum up, the above three methods can be used to determine whether a certain value exists in the array. If you want to check if a value exists, you can use the in_array or array_search function; if you want to check if an array element exists, you can use the isset function. For different usage scenarios, we can choose different methods to determine whether the array contains a specific value.

The above is the detailed content of How to determine if there is a certain value 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