ensureKeyValue.php
Table of Contents
Functions
- ensureKeyValue() : array<string|int, mixed>|object
- Ensures that one or more keys or properties exist in an array or object.
Functions
ensureKeyValue()
Ensures that one or more keys or properties exist in an array or object.
ensureKeyValue(array<string|int, mixed>|object $document, string|array<string|int, mixed> $keys[, mixed $default = null ][, string $separator = '.' ][, bool|null $isArray = null ][, bool $enforce = false ]) : array<string|int, mixed>|object
Missing keys are created and initialized. Defaults can be defined globally or per key. Nested keys are supported using a separator (default: '.'). Intermediate path segments are created automatically if missing.
Features:
- Specific Defaults: Pass an associative array to
$keysto set different defaults per key. - Lazy Loading: Pass a
Closureas a default value to calculate it only if needed. - Typed Properties: With
$enforce = true, strictly checks initialized state of typed properties.
Parameters
- $document : array<string|int, mixed>|object
-
The target document (array or object).
- $keys : string|array<string|int, mixed>
-
A single key, a list of keys
['a', 'b'], or an associative map['key' => 'default']. - $default : mixed = null
-
Global default value (or Closure) for keys without a specific default. Default is
null. - $separator : string = '.'
-
Separator used to split nested keys. Default is '.'.
- $isArray : bool|null = null
-
Optional: true for array mode, false for object mode, null to auto-detect.
- $enforce : bool = false
-
Force initialization of non-initialized typed properties. Default: false.
Tags
Return values
array<string|int, mixed>|object —The updated document with ensured keys.