rfc:array_only_except

PHP RFC: array_only() and array_except()

  • Version: 0.9
  • 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() and array_except() because developers repeatedly re‑implement this logic. This RFC proposes adding native PHP functions for these common operations.

  • function array_only(array $array, array $keys): array {}
  • function array_except(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($data, ['id', 'email']);
// Output
// ['id' => 1, 'email' => 'name@test.com']
 
array_except($data, ['email']);
 
// Output
// ['id' => 1, 'name' => 'Ali']
 
?>

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

Non-Existing Keys

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

Numeric Keys

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

Backward Incompatible Changes

None

Proposed PHP Version(s)

PHP 8.6

RFC Impact

This RFC adds new functions, which may conflict with userland helpers using the same names. Such cases can be resolved by replacing them with the native equivalents, as the behavior is compatible.

There is no impact on SAPIs or on OPcache.

Voting Choices

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

Implement $feature as outlined in the RFC?
Real name Yes No Abstain
Final result: 0 0 0
This poll has been closed.

Patches and Tests

Links to proof of concept PR.

If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.

Implementation

After the RFC is implemented, this section should contain:

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

References

Links to external references, discussions, or RFCs.

Rejected Features

Keep this updated with features that were discussed on the mail lists.

Changelog

If there are major changes to the initial proposal, please include a short summary with a date or a link to the mailing list announcement here, as not everyone has access to the wikis' version history.

rfc/array_only_except.txt · Last modified: by arshidkv12