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.
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'] ?>
<?php array_only([], ['a']); // [] array_except([], ['a']); // [] array_only(['a'=>1], []); // [] array_except(['a'=>1], []); // ['a'=>1] ?>
<?php array_only(['a'=>1], ['b']); // [] array_except(['a'=>1], ['b']); // ['a'=>1] ?>
<?php $a = [10, 20, 30, 40]; array_only($a, [1, 3]); // [1 => 20, 3 => 40] array_except($a, [0, 2]); // [1 => 20, 3 => 40] ?>
None
PHP 8.6
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.
Primary Vote requiring a 2/3 majority to accept the RFC:
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.
After the RFC is implemented, this section should contain:
Links to external references, discussions, or RFCs.
Keep this updated with features that were discussed on the mail lists.
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.