Hello Internals,
I would like to propose a new RFC titled array_only() and array_except().
RFC: https://wiki.php.net/rfc/array_only_except
https://wiki.php.net/rfc/array_only_except?utm_source=chatgpt.com
This RFC proposes adding two utility functions to the PHP core:
• array_only(array $array, array $keys): array
• array_except(array $array, array $keys): array
These functions allow selecting or excluding specific keys from an array.
Similar helpers are commonly used in frameworks, and adding native support
would improve readability and consistency in userland code.
Summary:
• array_only() returns a new array containing only the specified keys.
• array_except() returns a new array excluding the specified keys.
• Original array is not modified.
• Works with associative and numeric keys.
• No impact on SAPIs or on OPcache.
I would appreciate feedback and suggestions from the community.
If there is general agreement, I plan to move the RFC forward.
Thanks,
Muhammed Arshid KV
Le 20 févr. 2026 à 10:25, Muhammed Arshid KV arshidkv12@gmail.com a écrit :
Hello Internals,
I would like to propose a new RFC titled array_only() and array_except().
RFC: https://wiki.php.net/rfc/array_only_except https://wiki.php.net/rfc/array_only_except?utm_source=chatgpt.com
This RFC proposes adding two utility functions to the PHP core:• array_only(array $array, array $keys): array
• array_except(array $array, array $keys): arrayThese functions allow selecting or excluding specific keys from an array.
Similar helpers are commonly used in frameworks, and adding native support would improve readability and consistency in userland code.
Summary:
• array_only() returns a new array containing only the specified keys.
• array_except() returns a new array excluding the specified keys.
• Original array is not modified.
• Works with associative and numeric keys.
• No impact on SAPIs or on OPcache.I would appreciate feedback and suggestions from the community.
If there is general agreement, I plan to move the RFC forward.Thanks,
Muhammed Arshid KV
Hi,
The principle of the RFC is interesting to me, as it is a situation I encounter from time to time. Two remarks:
- It may be interesting to compare with currently available solutions. Currently, I use
array_intersect_keyandarray_diff_keywhen I need the semantics of proposedarray_onlyandarray_except:
array_intersect_key($data, ['id' => null, 'email' => null]);
- I think that the name of the new functions ought to make evident that we are filtering on keys and not on something else. I suggest:
array_only_keysandarray_except_keys.
—Claude
It seems clear to me that this proposal was under-researched because the
RFC doesn't even acknowledge the fact that PHP already has native functions
which serve the exact same purpose (and they have more indicative names).I'm not a voter, but I would vote No on this one because the new functions
aren't adding significant value to the language.Mick
Hi,
Thanks for the feedback.
-
Yes, *array_intersect_key() *and *array_diff_key() *can be used today,
but the RFC proposes dedicated functions for better readability, less
boilerplate, and potentially better performance and lower memory usage by
avoiding temporary arrays. -
Good point about naming. Making the key-filtering behavior explicit
(e.g., array_only_keys and array_except_keys) improves clarity and
avoids confusion.
Le 20 févr. 2026 à 10:25, Muhammed Arshid KV arshidkv12@gmail.com a
écrit :Hello Internals,
I would like to propose a new RFC titled array_only() and array_except()
.RFC: https://wiki.php.net/rfc/array_only_except
https://wiki.php.net/rfc/array_only_except?utm_source=chatgpt.comThis RFC proposes adding two utility functions to the PHP core:
• array_only(array $array, array $keys): array
• array_except(array $array, array $keys): arrayThese functions allow selecting or excluding specific keys from an array.
Similar helpers are commonly used in frameworks, and adding native support
would improve readability and consistency in userland code.Summary:
• array_only() returns a new array containing only the specified keys.
• array_except() returns a new array excluding the specified keys.
• Original array is not modified.
• Works with associative and numeric keys.
• No impact on SAPIs or on OPcache.I would appreciate feedback and suggestions from the community.
If there is general agreement, I plan to move the RFC forward.Thanks,
Muhammed Arshid KVHi,
The principle of the RFC is interesting to me, as it is a situation I
encounter from time to time. Two remarks:
- It may be interesting to compare with currently available solutions.
Currently, I usearray_intersect_keyandarray_diff_keywhen I need
the semantics of proposedarray_onlyandarray_except:array_intersect_key($data, ['id' => null, 'email' => null]);
- I think that the name of the new functions ought to make evident that
we are filtering on keys and not on something else. I suggest:
array_only_keysandarray_except_keys.—Claude