Home >Backend Development >PHP Problem >How to determine if an array has a key in php

How to determine if an array has a key in php

PHPz
PHPzOriginal
2023-04-23 10:13:43739browse

In PHP development, it is usually a common operation to determine whether a certain key exists in an array. This article will introduce in detail the method of determining whether a certain key exists in an array in PHP.

Method 1: Use the array_key_exists function

The array_key_exists function is a function used to check whether the specified key exists in the array. It returns a Boolean value.

Syntax:

bool array_key_exists (mixed $key, array $array)

Among them, key is the key name to be checked, and array is the key name to be checked. Array to check.

Sample code:

$arr = array(
    'name' => 'John',
    'age'  => 20,
);
 
if (array_key_exists('name', $arr)) {
    echo "Name exists in array"; 
} else {
    echo "Name does not exist in array";
}

Output result:

Name exists in array

Method 2: Use isset function

isset function can also be used to check whether an array exists as specified key, which returns a Boolean value.

Syntax:

bool isset ( mixed $var [, mixed $... ] )

where var is the variable to be checked or Arrays can check multiple variables or arrays at the same time.

Sample code:

$arr = array(
    'name' => 'John',
    'age'  => 20,
);
 
if (isset($arr['name'])) {
    echo "Name exists in array"; 
} else {
    echo "Name does not exist in array";
}

Output result:

Name exists in array

Method 3: Use in_array function

The in_array function can be used to check whether an array exists Value can also be used to check whether a certain key exists in an array. When using the in_array function to check whether a key exists, you need to put the key name into the array for checking.

Syntax:

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

Among them, needle is the key The value or key name to be checked, haystack is the array to be checked, and strict is whether to use strict mode checking.

Sample code:

$arr = array(
    'name' => 'John',
    'age'  => 20,
);
 
if (in_array('name', array_keys($arr))) {
    echo "Name exists in array"; 
} else {
    echo "Name does not exist in array";
}

Output result:

Name exists in array

Method 4: Use array_key_first and array_key_last functions

The array_key_first function can be used to obtain the first element of an array A key, the array_key_last function can be used to obtain the last key of an array. These two functions can be used to determine whether a certain key exists in an array.

Syntax:

mixed array_key_first (array $array)

mixed array_key_last (array $array)

Among them, array is the array to obtain the first or last key.

Sample code:

$arr = array(
    'name' => 'John',
    'age'  => 20,
);
 
if (array_key_first($arr) === 'name') {
    echo "Name is the first key in array"; 
}
 
if (array_key_last($arr) === 'age') {
    echo "Age is the last key in array"; 
}

Output result:

Name is the first key in array
Age is the last key in array

The above are four methods for judging whether the specified key exists in the array. Developers can choose the appropriate method according to the specific scenario. use.

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