rfc:array_only_except

PHP RFC: array_only_keys() and array_except_keys()

  • Version: 1.0
  • Date: 2026-20-02
  • Author: Muhammed Arshid KV, arshidkv12@gmail.com
  • Status: Under Discussion
  • Implementation: TBD

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' => 'Name here']
 
?>

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.

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:

Add array_only_keys() and array_except_keys()
Real name Yes No Abstain
Final result: 0 0 0
This poll has been closed.

Patches and Tests

None

Implementation

After the RFC is implemented, this section should contain:

  1. the version(s) it was merged into: 8.6
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Rejected Features

None

Changelog

1.0: Initial version under discussion 1.1 Changed function names array_only() and array_except()

rfc/array_only_except.txt · Last modified: by arshidkv12