Home  >  Article  >  Backend Development  >  How to determine whether the value of an array exists in php

How to determine whether the value of an array exists in php

PHPz
PHPzOriginal
2023-04-23 09:12:40580browse

In PHP programming, array is a very commonly used data structure. In some cases, we need to detect whether an array already contains a certain value. This is also a very basic requirement in array operations. This article will introduce you to the value existence judgment of PHP arrays.

Key-value pair characteristics of PHP array

In PHP, array is a very basic data type. It consists of key-value pairs, where the key is a string or number, and the value can be any type of data, including integers, floating point numbers, Boolean types, strings, objects, arrays, etc.

When using arrays in PHP, we can define an array of key-value pairs as follows:

$arr = array(
    "key1" => "value1",
    "key2" => "value2",
    "key3" => "value3"
);

In this way, we create an array containing three key-value pairs, each of which The value corresponding to each key is the string data shown above. We can use subscripts to get the value of an element in this array, like this:

echo $arr["key2"]; // 输出 value2

It should be noted that in PHP, when the array subscript is a number, you can use the abbreviation Define the array:

$arr = [1, 2, 3];

The above example is equivalent to the following code:

$arr = array(1, 2, 3);

PHP array detects whether the value exists

When we have a PHP array and need to know the array What should we do if it contains a specific value? PHP provides some very convenient functions and statements that can help us complete this task quickly.

  1. Using the in_array() function

The in_array() function in PHP can be used to check whether the array contains a specific value. This function takes two parameters, the first parameter is the value we are looking for and the second parameter is the array to check. The function returns a boolean value, true if found, false otherwise. For example:

$arr = array("apple", "banana", "orange");
if (in_array("banana", $arr)) {
    echo "找到了";
} else {
    echo "没找到";
}

In this example, we define an array containing three string elements, and then use the in_array() function to check whether the array contains the string "banana". Since the string "banana" exists in this array, the in_array() function will return true and eventually output "found".

  1. Using the array_search() function

The array_search() function in PHP can also be used to find a specific value in an array. The difference between this function and the in_array() function is that it not only helps us find the position of this value in the array, but also returns this position. If found, returns the key value of the element; if not found, returns false. For example:

$arr = array("apple", "banana", "orange");
$index = array_search("banana", $arr);
if ($index !== false) {
    echo "找到了,位置为:" . $index;
} else {
    echo "没找到";
}

In this example, we also define an array containing three string elements, and use the array_search() function to find whether the array contains the string "banana". If found, the function will return the key value of the element in the array (that is, 1) and output "Found, position: 1". Otherwise, false will be returned and "not found" will be output.

  1. Use isset() function

In addition to the in_array() and array_search() functions introduced above, the isset() function in PHP can also be used for checking Whether a value in the array exists. The syntax of this function is relatively simple, you only need to pass in the key value to be checked. Returns true if the key value exists, otherwise returns false. For example:

$arr = array("apple", "banana", "orange");
if (isset($arr[1])) {
    echo "存在";
} else {
    echo "不存在";
}

In this example, we use the isset() function to check whether the element with index 1 exists in the array. Since there is indeed an element with index 1 in the array, the isset() function will return true and eventually output "exists". If the corresponding value is false, the isset() function will also return true, so when judging the value of an element in the array, use the isset() function with caution.

Summary

In PHP, array is a very basic and commonly used data structure. When we need to check whether a value in an array of key-value pairs exists, we can use functions and statements such as in_array(), array_search(), and isset(). These methods are very commonly used, so mastering them is very helpful for PHP programming. At the same time, it should be noted that if we want to determine whether the value of an element in the array is false, do not use the isset() function because it will return true.

The above is the detailed content of How to determine whether the value of 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