Oihana PHP

hasKeyValue.php

Table of Contents

Functions

hasKeyValue()  : bool
Checks whether a given key or property exists in an array or object, including nested paths.

Functions

hasKeyValue()

Checks whether a given key or property exists in an array or object, including nested paths.

hasKeyValue(array<string|int, mixed>|object $document, string $key[, string $separator = '.' ][, bool|null $isArray = null ]) : bool

This helper determines if the specified key exists in the given document (array or object). It supports nested access via a separator (default is '.') and can optionally force the document type (array or object).

If any part of the path does not exist, false is returned. This function does not rely on __get() or __isset() magic methods for objects.

Parameters
$document : array<string|int, mixed>|object

The document (array or object) to inspect.

$key : string

The key or property path to check. Supports nesting with separator.

$separator : string = '.'

Separator used for nested paths. Default is '.'.

$isArray : bool|null = null

Optional: true if document is an array, false if object, null to auto-detect.

Tags
throws
InvalidArgumentException

If the document or key is invalid.

author

Marc Alcaraz (ekameleon)

since
1.0.0
example
$doc = ['name' => 'Alice'];
hasKeyValue($doc, 'name'); // true
hasKeyValue($doc, 'age');  // false
$doc = ['user' => ['name' => 'Alice']];
hasKeyValue($doc, 'user.name'); // true
hasKeyValue($doc, 'user.age');  // false
$doc = (object)['user' => (object)['name' => 'Alice']];
hasKeyValue($doc, 'user.name'); // true
hasKeyValue($doc, 'user.age');  // false
$doc = [];
hasKeyValue($doc, 'config.debug.enabled'); // false
$doc = (object)[];
hasKeyValue($doc, 'meta.version.major'); // false
Return values
bool

True if the full key path exists, false otherwise.


        
On this page

Search results