Oihana PHP

deleteKeyValue.php

Table of Contents

Functions

deleteKeyValue()  : array<string|int, mixed>|object
Deletes a value from an array or object using a dot-notated key path.

Functions

deleteKeyValue()

Deletes a value from an array or object using a dot-notated key path.

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

This utility supports:

  • Single key deletion (string)
  • Multiple key deletion (array of strings)
  • Flat keys (e.g., "name")
  • Nested keys (e.g., "user.profile.name")
  • Wildcard deletion for sub-containers (e.g., "user.*")
  • Global wildcard "*" to clear the entire document

The input can be an associative array or a stdClass-like object. Intermediate paths are ensured to exist before deletion.

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

The data source (array or object) to operate on.

$key : string|array<string|int, mixed>

The key path(s) to delete (e.g. "foo.bar" or ["foo.bar", "baz.*"]).

$separator : string = '.'

The separator used to split the key path. Defaults to '.'.

$isArray : bool|null = null

Optional: force array (true) or object (false) mode; if null, auto-detects.

$strict : bool = false

If true, throws exception when key doesn't exist. Default: false.

Tags
throws
InvalidArgumentException

If input is not array/object, if key is invalid, or if path traversal encounters a type mismatch. In strict mode, also throws if key doesn't exist.

example

Delete an single array key :

$doc = ['name' => 'Alice', 'age' => 30];
$doc = deleteKeyValue( $doc , 'age' ) ;
// Result: ['name' => 'Alice']
example

Delete multiple keys at once:

$doc = ['name' => 'Alice', 'age' => 30, 'email' => 'a@b.c'];
$doc = deleteKeyValue($doc, ['age', 'email']);
// Result: ['name' => 'Alice']
example

Delete multiple nested keys:

$doc = ['user' => ['name' => 'Alice', 'email' => 'a@b.c', 'age' => 30]];
$doc = deleteKeyValue($doc, ['user.name', 'user.age']);
// Result: ['user' => ['email' => 'a@b.c']]
example

Mix of simple and wildcard deletions:

$doc =
[
    'name' => 'Alice',
    'meta' => ['a' => 1, 'b' => 2],
    'config' => ['x' => 10, 'y' => 20]
];
$doc = deleteKeyValue($doc, ['name', 'meta.*']);
// Result: ['meta' => [], 'config' => ['x' => 10, 'y' => 20]]
example

Delete all properties with a wildcard key expression.

$doc = ['meta' => ['a' => 1, 'b' => 2]];
$doc = deleteKeyValue($doc, 'meta.*');
// Result: ['meta' => []]
example

Delete all object properties:

$doc = (object)['user' => (object)['name' => 'Alice', 'email' => 'a@b.c']];
$doc = deleteKeyValue($doc, 'user.*');
// Result: (object)['user' => (object)[]]
example

Non-strict mode (default) - ignore missing keys:

$doc = ['name' => 'Alice', 'age' => 30];
$doc = deleteKeyValue($doc, ['age', 'email', 'phone']); // no error
// Result: ['name' => 'Alice']
example

Strict mode - throws exception for missing keys:

$doc = ['name' => 'Alice', 'age' => 30];
try {
$doc = deleteKeyValue($doc, ['age', 'email'], strict: true);
} catch (InvalidArgumentException $e) {
echo $e->getMessage(); // "Key 'email' does not exist."
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string|int, mixed>|object

The updated document after deletion.


        
On this page

Search results