Oihana PHP

delete.php

Table of Contents

Functions

delete()  : mixed
Unset a key or nested key in an array using dot notation.

Functions

delete()

Unset a key or nested key in an array using dot notation.

delete(mixed $target, string|array<string|int, mixed> $key[, string $separator = '.' ]) : mixed
  • If the key is '*', clears the whole array.
  • Supports string path ("a.b.c") or array path (['a','b','c']).
  • If the path is partially invalid, the array is returned unchanged.
Parameters
$target : mixed

The array to modify (ignored if not an array).

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

The key path to delete.

$separator : string = '.'

The separator for string key paths (default: '.').

Tags
example
$data =
[
    'user' =>
    [
        'profile' =>
        [
            'name' => 'Alice',
            'age' => 30
        ],
        'active' => true
    ]
];

// Remove a nested key
$result = delete($data, 'user.profile.age');
print_r($result);
// [
//     'user' => [
//         'profile' => ['name' => 'Alice'],
//         'active'  => true
//     ]
// ]

// Remove a top-level key
$result = delete($data, 'user.active');

// Remove all keys
$result = delete($data, '*');
print_r($result); // []

// Using array path
$result = delete($data, ['user', 'profile', 'name']);
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
mixed

The modified array (or original if not an array).


        
On this page

Search results