Home  >  Article  >  Backend Development  >  How to determine if a certain key exists in an array in PHP

How to determine if a certain key exists in an array in PHP

PHPz
PHPzOriginal
2023-04-18 10:24:21617browse

When writing PHP programs, you often need to operate on arrays. When operating on an array, we inevitably need to judge a certain element in the array, that is, whether a certain key exists. Therefore, this article will introduce how to determine whether a certain key exists in an array in PHP, and how to perform corresponding operations based on the determination results.

  1. Judge whether a certain key exists in an array

In PHP, you can use the following methods to judge whether a certain key exists in an array:

1.1 Use the isset() function

isset() function is used to check whether the variable is declared and the value is not null. When determining whether a certain key exists in the array, we can use this function to determine whether the corresponding key exists in the array. The sample code is as follows:

$my_array = array("foo" => "bar", "hello" => "world");
if (isset($my_array["foo"])) {
    echo "键foo存在";
} else {
    echo "键foo不存在";
}

The output result is: key foo exists.

1.2 Use the array_key_exists() function

The array_key_exists() function is used to check whether the given key name or index exists in the array. Unlike isset(), array_key_exists() also returns true when the value corresponding to the key is null. The sample code is as follows:

$my_array = array("foo" => "bar", "hello" => "world");
if (array_key_exists("foo", $my_array)) {
    echo "键foo存在";
} else {
    echo "键foo不存在";
}

The output result is: key foo exists.

1.3 Use the in_array() function

The in_array() function is used to check whether a value exists in the array. If you need to check the key value, you need to store the key name and value in two arrays respectively. The sample code is as follows:

$my_array = array("foo" => "bar", "hello" => "world");
if (in_array("bar", $my_array, true)) {
    echo "键foo存在";
} else {
    echo "键foo不存在";
}

The output result is: key foo exists.

  1. Perform corresponding operations based on the judgment results

After judging whether a certain key exists in the array, it is also important to perform corresponding operations based on the judgment results. The following are two common approaches:

2.1 Directly operate on keys

If we need to operate on a key that exists in the array, we can directly use the key to execute the corresponding code. The sample code is as follows:

$my_array = array("foo" => "bar", "hello" => "world");
if (isset($my_array["foo"])) {
    echo $my_array["foo"];
} else {
    echo "键foo不存在";
}

The output result is: bar.

2.2 Set a default value for a non-existent key

If we need to execute different codes based on whether the key exists, we can set a default value for the non-existent key. The sample code is as follows:

$my_array = array("foo" => "bar", "hello" => "world");
$default_value = "默认值";
if (isset($my_array["foo"])) {
    echo $my_array["foo"];
} else {
    echo $default_value;
}

In this code, if the key foo exists, the value corresponding to the key is output; otherwise, the default value is output. The output result is: bar.

Summary

This article introduces three methods to determine whether a key exists in an array in PHP, and gives two common methods for determining the results. By applying these methods, we can better operate on arrays and improve the efficiency of the program.

The above is the detailed content of How to determine if a certain key exists in an array 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