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

Apr 23, 2023 am 10:13 AM

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";
}
Copy after login

Output result:

Name exists in array
Copy after login
Copy after login
Copy after login

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";
}
Copy after login

Output result:

Name exists in array
Copy after login
Copy after login
Copy after login

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";
}
Copy after login

Output result:

Name exists in array
Copy after login
Copy after login
Copy after login

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"; 
}
Copy after login

Output result:

Name is the first key in array
Age is the last key in array
Copy after login

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 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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles