# [RFC] array_only() and array_except() – Utility functions for array key filtering

- Thread: https://externals.io/message/130102
- Original archive: https://news-web.php.net/php.internals/130102
- Started by Muhammed Arshid KV on 2026-02-20T09:25:53+00:00
- Messages: 14
- RFC: array_only_keys() and array_except_keys() (Declined) — https://externals.io/rfc/array_only_except.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #130102 — Muhammed Arshid KV — 2026-02-20T09:25:53+00:00

URL: https://externals.io/message/130102#130102

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

---

## #130103 — Claude Pache — 2026-02-20T10:08:38+00:00

URL: https://externals.io/message/130102#130103 · In reply to: #130102

> 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): 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
> 

Hi,

The principle of the RFC is interesting to me, as it is a situation I encounter from time to time. Two remarks:

1. It may be interesting to compare with currently available solutions. Currently, I use  `array_intersect_key` and `array_diff_key` when I need the semantics of proposed `array_only` and `array_except`:

```php
array_intersect_key($data, ['id' => null, 'email' => null]);
```

2. 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_keys` and `array_except_keys`.

—Claude

---

## #130106 — mickmackusa — 2026-02-20T13:13:42+00:00

URL: https://externals.io/message/130102#130106 · In reply to: #130103

>
> 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

---

## #130111 — Muhammed Arshid KV — 2026-02-20T17:37:38+00:00

URL: https://externals.io/message/130102#130111 · In reply to: #130103

Hi,

Thanks for the feedback.

1. 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.

2. 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.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
>
>
> Hi,
>
> The principle of the RFC is interesting to me, as it is a situation I
> encounter from time to time. Two remarks:
>
> 1. It may be interesting to compare with currently available solutions.
> Currently, I use  `array_intersect_key` and `array_diff_key` when I need
> the semantics of proposed `array_only` and `array_except`:
>
> ```php
> array_intersect_key($data, ['id' => null, 'email' => null]);
> ```
>
> 2. 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_keys` and `array_except_keys`.
>
> —Claude

---

## #130129 — tim@bastelstu.be — 2026-02-22T18:25:21+00:00

URL: https://externals.io/message/130102#130129 · In reply to: #130111

Hi

The same comments as with your other RFC apply here.


> 1. 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.

Can you elaborate on what you mean by “lower memory usage by avoiding 
temporary arrays”?

> 2. Good point about naming. Making the key-filtering behavior explicit
> (e.g., *array_only_keys* and *array_except_keys*) improves clarity and
> avoids confusion.

I don't think that `array_(only|except)_keys()` is particularly clear. 
 From the naming I would expect it to return the array keys / values 
(i.e. do the same as `array_keys()` and `array_values()` respectively).

Best regards
Tim Düsterhus

---

## #130130 — Muhammed Arshid KV — 2026-02-22T18:58:59+00:00

URL: https://externals.io/message/130102#130130 · In reply to: #130129

Hi,

Here is a simple example of an `array_only()` implementation using existing
PHP functions:

```php
function array_only(array $input, array $keys): array {
    return array_intersect_key($input, 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.

Thanks.




> Hi
>
> The same comments as with your other RFC apply here.
>
> 
> > […]
>
> Can you elaborate on what you mean by “lower memory usage by avoiding
> temporary arrays”?
>
> > […]
>
> I don't think that `array_(only|except)_keys()` is particularly clear.
>  From the naming I would expect it to return the array keys / values
> (i.e. do the same as `array_keys()` and `array_values()` respectively).
>
> Best regards
> Tim Düsterhus

---

## #130209 — tim@bastelstu.be — 2026-03-01T14:10:36+00:00

URL: https://externals.io/message/130102#130209 · In reply to: #130130

Hi

Am 2026-02-22 19:58, schrieb Muhammed Arshid KV:
> Here is a simple example of an `array_only()` implementation using 
> existing
> PHP functions:
> 
> ```php
> function array_only(array $input, array $keys): array {
>     return array_intersect_key($input, array_flip($keys));
> }
> ```
> 
> This works, but `array_flip($keys)` creates an extra temporary hash 
> table.
> So peak memory becomes: input + keys + flipped array + result.

That is true for this specific implementation of a “wrapper function”. 
It is not necessarily true for other implementations, e.g. simply using 
the functions directly instead of creating a wrapper for two method 
calls. `array_flip()` supports compile-time evaluation, so you can just 
write:

     $dataWithOnlyIdAndEmail = \array_intersect_key($data, 
\array_flip(['id', 'email']));

and OPcache will make sure to rewrite it to:

     $dataWithOnlyIdAndEmail = \array_intersect_key($data, ['id' => 0, 
'email' => 1]);

avoiding the intermediate array.

Best regards
Tim Düsterhus

PS: It appears that the RFC is not listed in the overview at 
https://wiki.php.net/rfc.

---

## #130210 — Muhammed Arshid KV — 2026-03-01T14:22:46+00:00

URL: https://externals.io/message/130102#130210 · In reply to: #130209

Hi Tim,

Thanks for the explanation.

In this example,

$dataWithOnlyIdAndEmail = \array_intersect_key($data, ['id' => 0, 'email'
=> 1]);


the second argument is an associative array.

But in my *array_only(array $input, array $keys) *function, *$keys* is
a *numeric
array.* Working with a *numeric array* may be slightly faster than building
an associative array first.


Just wanted to clarify that point.


Best regards,

Muhammed Arshid KV



> Hi
>
> Am 2026-02-22 19:58, schrieb Muhammed Arshid KV:
> > […]
>
> That is true for this specific implementation of a “wrapper function”.
> It is not necessarily true for other implementations, e.g. simply using
> the functions directly instead of creating a wrapper for two method
> calls. `array_flip()` supports compile-time evaluation, so you can just
> write:
>
>      $dataWithOnlyIdAndEmail = \array_intersect_key($data,
> \array_flip(['id', 'email']));
>
> and OPcache will make sure to rewrite it to:
>
>      $dataWithOnlyIdAndEmail = \array_intersect_key($data, ['id' => 0,
> 'email' => 1]);
>
> avoiding the intermediate array.
>
> Best regards
> Tim Düsterhus
>
> PS: It appears that the RFC is not listed in the overview at
> https://wiki.php.net/rfc.

---

## #130256 — Muhammed Arshid KV — 2026-03-05T05:23:15+00:00

URL: https://externals.io/message/130102#130256 · In reply to: #130210

Hi,

I think the RFC *“array_only_except”* is ready to move to the voting phase.

If there are no further objections or required changes, I would like to
proceed with starting the vote.

Thanks.

---

## #130268 — Ben Ramsey — 2026-03-07T22:54:06+00:00

URL: https://externals.io/message/130102#130268 · In reply to: #130256

> Hi,
> 
> I think the RFC *“array_only_except”* is ready to move to the voting 
> phase.
> 
> If there are no further objections or required changes, I would like 
> to proceed with starting the vote.
> 
> Thanks.
> 


Unless I've missed something, I don't think the RFC has addressed any of
the feedback from the discussion on this list.

Claude Pache suggested:


> It may be interesting to compare with currently available solutions.


And Mick suggested something similar:


> the RFC doesn't even acknowledge the fact that PHP already has 
> native functions which serve the exact same purpose


On the list you acknowledged `array_intersect_key()` and
`array_diff_key()`, saying:


> the RFC proposes dedicated functions for better readability, less
> boilerplate, and potentially better performance and lower memory
> usage.


I would have expected all of this to be added to the RFC to provide
further rationale for why these new functions are needed, along with
some numbers to back up the better performance and lower memory usage
claims.

In another reply to the list, you seemed to indicate that you were going
to change the names of the functions in the RFC:


> Good point about naming. Making the key-filtering behavior explicit
> (e.g., array_only_keys and array_except_keys) improves clarity and
> avoids confusion.


Did you decide against changing the names in the RFC? I don't see any
updates to that effect.

I'd like to see the feedback addressed within the RFC before opening it
for a vote. It think it would make the RFC stronger.

Cheers,
Ben

---

## #130272 — Muhammed Arshid KV — 2026-03-08T17:06:16+00:00

URL: https://externals.io/message/130102#130272 · In reply to: #130268

Hi

>Unless I've missed something, I don't think the RFC has addressed any of
the feedback from the discussion on this list.


Thank you for the feedback and for taking the time to review the RFC.

I have updated the RFC to address the points raised in the discussion.

Please have a look when you have a chance and let me know if anything still
needs improvement.


Regards

Muhammed Arshid

---

## #130273 — Ben Ramsey — 2026-03-08T20:14:21+00:00

URL: https://externals.io/message/130102#130273 · In reply to: #130272

> Hi
> 
> > […]
> the feedback from the discussion on this list.
> 
> 
> Thank you for the feedback and for taking the time to review the RFC.
> 
> I have updated the RFC to address the points raised in the discussion.
> 
> Please have a look when you have a chance and let me know if anything still
> needs improvement.
> 
> 
> Regards
> 
> Muhammed Arshid
> 


Thanks!

One more thing I'd ask to see in the RFC: can you provide examples of 
similar functions that are implemented in PHP userland 
libraries/frameworks, as well as point to examples in other programming 
languages that provide similar functionality? It would be good to have a 
basis for comparison with how others are already approaching this problem.

Cheers,
Ben

---

## #130276 — Muhammed Arshid KV — 2026-03-09T05:27:24+00:00

URL: https://externals.io/message/130102#130276 · In reply to: #130273

Hi



> 
>
> Thanks!
>
> One more thing I'd ask to see in the RFC: can you provide examples of
> similar functions that are implemented in PHP userland
> libraries/frameworks, as well as point to examples in other programming
> languages that provide similar functionality? It would be good to have a
> basis for comparison with how others are already approaching this problem.
>
> Cheers,
> Ben
>
>

I’ve added examples from the Laravel PHP framework.

Cheers,
Arshid

---

## #130287 — Muhammed Arshid KV — 2026-03-10T22:37:52+00:00

URL: https://externals.io/message/130102#130287 · In reply to: #130276

Hi, it appears the RFC “array_only_except” is ready for voting. I’d
appreciate your feedback.


Regards
