method_check_array_key()
method_check_array_key( $item, $key )
Checks whether an array key exists and has a non‑empty value.
Description
A safe, concise way to verify that $item is an array, that $key exists within it, and that the value at that key is not empty. This is the most frequently used utility function in Method — it appears in virtually every block render callback and helper function.
Parameters
$item (mixed) :
The variable to check. If not an array, returns false.
$key (string|int) :
The array key to check.
Return
(bool) true if $item is an array, $key exists, and $item[$key] is non‑empty. false otherwise.
Source
PHP
function method_check_array_key( $item, $key ) {
return is_array( $item ) && isset( $item[ $key ] ) && ! empty( $item[ $key ] );
}File: lib/helper-functions.php
Usage
PHP
if ( method_check_array_key( $block_attributes, 'link' ) ) {
$url = $block_attributes['link']['url'];
}
if ( method_check_array_key( $options, 'bc_home' ) ) {
// Include home link in breadcrumb
}Notes
- Replaces the deprecated
method_check_key()for array contexts. - Uses
empty()internally, so values like0,'',null,false, and[]all returnfalse.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Replacement for method_check_key(). |