Take the State of PHP 2026 Survey →

array_only_keys() and array_except_keys() View as Markdown

Declined
Authors
Muhammed Arshid KV, arshidkv12@gmail.com
Date
2026-02-20
Version
1.2
Implementation
TBD
Source
wiki.php.net/rfc/array_only_except

Vote

Add array_only_keys() and array_except_keys() closed
Yes
2
No
9
Abstain
9
20 voters
alcaeus Abstain 2026-03-13 08:02
alexandredaubois No 2026-03-19 09:55
ashnazg Yes 2026-03-23 16:25
crell Yes 2026-03-12 17:58
cschneid No 2026-03-12 23:30
daniels No 2026-03-21 19:00
derick Abstain 2026-03-18 18:13
edorian No 2026-03-19 19:04
ericmann No 2026-03-19 03:38
galvao No 2026-03-19 04:35
iliaa Abstain 2026-03-14 13:56
ilutov Abstain 2026-03-18 15:05
mbeccati No 2026-03-13 06:44
ocramius Abstain 2026-03-12 17:16
pollita No 2026-03-25 19:36
ramsey Abstain 2026-03-21 16:58
svpernova09 Abstain 2026-03-23 15:23
theodorejb Abstain 2026-03-22 03:53
timwolla No 2026-03-12 22:19
weierophinney Abstain 2026-03-14 02:58

Discussions on the mailing list

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

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
$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
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
array_only_keys([], ['a']);        // []
array_except_keys([], ['a']);      // []
 
array_only_keys(['a'=>1], []);     // []
array_except_keys(['a'=>1], []);   // ['a'=>1]
?>

Non-Existing Keys

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

Numeric Keys

<?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