====== 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: 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 ==== - The functions return a new array. - The original array remains unchanged. - The original key order is preserved. - Missing keys are ignored silently. - Duplicate keys in $keys are ignored. - Works with both associative and numeric arrays. ==== Empty Input Conditions ==== 1], []); // [] array_except(['a'=>1], []); // ['a'=>1] ?> ==== Non-Existing Keys ==== 1], ['b']); // [] array_except(['a'=>1], ['b']); // ['a'=>1] ?> ==== Numeric Keys ==== 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: * Yes * No * Abstain ===== 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: - the version(s) it was merged into - a link to the git commit(s) - 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.