# RFC: array_only_keys() and array_except_keys()

- Status: Declined
- Authors: Muhammed Arshid KV, arshidkv12@gmail.com
- Date: 2026-02-20
- Version: 1.2
- Implementation: TBD
- Original: https://wiki.php.net/rfc/array_only_except
- Page: https://externals.io/rfc/array_only_except
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Add array_only_keys() and array_except_keys() (closed)

Yes: 2 · No: 9 · Abstain: 9

Voters: alcaeus (Abstain), alexandredaubois (No), ashnazg (Yes), crell (Yes), cschneid (No), daniels (No), derick (Abstain), edorian (No), ericmann (No), galvao (No), iliaa (Abstain), ilutov (Abstain), mbeccati (No), ocramius (Abstain), pollita (No), ramsey (Abstain), svpernova09 (Abstain), theodorejb (Abstain), timwolla (No), weierophinney (Abstain)

## Discussions on the mailing list

- [\[RFC\] array_only() and array_except() – Utility functions for array key filtering](https://externals.io/message/130102.md) — started on 2026-02-20, 14 messages
- [\[RFC\]\[VOTE\] array_only() and array_except() – Utility functions for array key filtering](https://externals.io/message/130297.md) — started on 2026-03-12, 4 messages

---

## Introduction

Working with arrays often requires selecting or excluding a subset of keys. Many frameworks provide helper functions like array\_only\_keys() and array\_except\_keys() because developers repeatedly re‑implement this logic. This RFC proposes adding native PHP functions for these common operations.

- function array\_only\_keys(array $array, array $keys): array {}
- function array\_except\_keys(array $array, array $keys): array {}

## Proposal

Add two new functions to the PHP core:

The usage of the methods to handle the keys will look like:

```php
<?php
$data = [
    'id' => 1,
    'name' => 'Name here',
    'email' => 'name@test.com',
];
 
array_only_keys($data, ['id', 'email']);
// Output
// ['id' => 1, 'email' => 'name@test.com']
 
array_except_keys($data, ['email']);
 
// Output
// ['id' => 1, 'name' => 'Ali']
 
?>
```

Here is a simple example of an array\_only\_keys() and array\_except\_keys implementation using existing PHP functions:

```php
<?php
function array_only_keys(array $input, array $keys): array {
    return array_intersect_key($input, array_flip($keys));
}
function array_except_keys(array $array, array $keys) {
    return array_diff_key($array, array_flip($keys));
}
```

This works, but `array_flip($keys)` creates an extra temporary hash table. So peak memory becomes: input + keys + flipped array + result.

In a native C implementation, we can iterate over `$keys` and copy values directly into the result without creating that temporary array. That means fewer allocations and lower peak memory usage, especially with large key sets or inside loops.

See the implementation in Laravel: <https://github.com/laravel/framework/blob/12.x/src/Illuminate/Collections/Arr.php#L724-L727>

Laravel Arr::except implementation: <https://github.com/laravel/framework/blob/12.x/src/Illuminate/Collections/Arr.php#L231-L236>

### Detailed Behavior

1. The functions return a new array.
2. The original array remains unchanged.
3. The original key order is preserved.
4. Missing keys are ignored silently.
5. Duplicate keys in $keys are ignored.
6. Works with both associative and numeric arrays.

### Empty Input Conditions

```php
<?php
array_only_keys([], ['a']);        // []
array_except_keys([], ['a']);      // []
 
array_only_keys(['a'=>1], []);     // []
array_except_keys(['a'=>1], []);   // ['a'=>1]
?>
```

### Non-Existing Keys

```php
<?php
 
array_only_keys(['a'=>1], ['b']);      // []
array_except_keys(['a'=>1], ['b']);    // ['a'=>1]
?>
```

### Numeric Keys

```php
<?php
$a = [10, 20, 30, 40];
 
array_only_keys($a, [1, 3]);
// [1 => 20, 3 => 40]
 
array_except_keys($a, [0, 2]);
// [1 => 20, 3 => 40]
?>
```

## Backward Incompatible Changes

None

## Proposed PHP Version(s)

PHP 8.6

## RFC Impact

### To Existing Extensions

These two new functions are added to ext-standard, where array\_only\_keys() and array\_except\_keys() and array\_only\_keys() and array\_except\_keys() also live.

## Voting Choices

Primary Vote requiring a 2/3 majority to accept the RFC:

**Vote: Add array\_only\_keys() and array\_except\_keys()** — Yes: 2 · No: 9 · Abstain: 9 (closed)

## Patches and Tests

None

## References

Current discussion: <https://externals.io/message/130102>

## Rejected Features

None

## Changelog

1.0: Initial version under discussion

1.1 Changed function names array\_only() and array\_except()

1.2 Laravel's link added
