Oihana PHP

removeKeys.php

Table of Contents

Functions

removeKeys()  : array<string|int, mixed>
Removes a set of keys from an array.

Functions

removeKeys()

Removes a set of keys from an array.

removeKeys(array<string|int, mixed> $array[, array<string|int, mixed> $keys = [] ][, bool $clone = false ]) : array<string|int, mixed>

This function removes the specified keys from the given array. If $clone is set to true, the original array is not modified; instead, a new array is returned. Otherwise, the original array is modified directly (passed by value).

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

The input array to modify or clone.

$keys : array<string|int, mixed> = []

An array of keys to remove from the input array.

$clone : bool = false

If true, operates on a copy of the array and returns it; otherwise, modifies the array in place (by value).

Tags
example
use function oihana\core\arrays\removeKeys;

$input = ['name' => 'Alice', 'email' => 'alice@example.com', 'age' => 30];

// Example 1: Remove one key (in-place behavior)
$result = removeKeys($input, ['email']);
// $result = ['name' => 'Alice', 'age' => 30]

// Example 2: Remove multiple keys
$result = removeKeys($input, ['name', 'age']);
// $result = ['email' => 'alice@example.com']

// Example 3: Use clone to preserve the original array
$original = ['a' => 1, 'b' => 2, 'c' => 3];
$copy = removeKeys($original, ['a'], true);
// $original remains unchanged: ['a' => 1, 'b' => 2, 'c' => 3]
// $copy = ['b' => 2, 'c' => 3]

// Example 4: Key not found (no error)
$result = removeKeys(['x' => 1], ['y']);
// $result = ['x' => 1]

// Example 5: Empty key list
$result = removeKeys(['foo' => 'bar'], []);
// $result = ['foo' => 'bar']
author

Marc Alcaraz (ekameleon)

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

The resulting array with specified keys removed.


        
On this page

Search results