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

From: Date: Sun, 22 Feb 2026 18:58:59 +0000
Subject: Re: [RFC] array_only() and array_except() – Utility functions for array key filtering
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-130130@lists.php.net to get a copy of this message
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. On Sun, 22 Feb 2026, 11:55 pm Tim Düsterhus, <tim@bastelstu.be> wrote: > Hi > > The same comments as with your other RFC apply here. > > On 2/20/26 18:37, Muhammed Arshid KV wrote: > > 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 >

« previous php.internals (#130130) next »