Hi,

After programming in PHP for two decades, my goal for 2023 is to try to 
contribute to the language.
The plan is to start small and, if successful, work my way up increasing 
complexity of proposals.
This topic has been chosen for starters, because IMO it strikes a good balance 
between simplicity and usefulness.
I should be able to implement the RFC myself, unless some deep OPcache/JIT 
nuances pop up.

Let me give you a brief overview of the problem and the proposed solution.
Function array_is_list() added in PHP 8.1 introduces the concept of a "list" – 
array having 0..count-1 indexes.
The function is awesome and array "lists" are completely compatible with all 
array_* functions!
However, function array_filter() exhibits a nuanced behavior when filtering 
lists.
For instance, it preserves array keys which may (or may not) create gaps in 
sequential indexes.
These gaps mean that a filtered list is not a list anymore as validated by 
array_is_list().

For example:
$originalList = ['first', '', 'last'];
$filteredList = array_filter($originalList);
var_export(filteredList);  // array(0 => 'first', 2 => 'last')
var_export(array_is_list($originalList));  // true
var_export(array_is_list($filteredList));  // false

The behavior is counterintuitive and can lead to subtle bugs, such as encoding 
issues:
echo json_encode($originalList);  // ["first", "", "last"]
echo json_encode($filteredList);  // {"0": "first", "2": "last"}

The workaround is to post-process the filtered array with array_values() to 
reset the indexes.
The proposal is to introduce a function array_filter_list() that would work 
solely on lists.
It will have the same signature as array_filter() and will always return a 
valid list.

See a draft RFC with more details here:
https://dev.to/sshymko/php-rfc-arrayfilterlist-function-35mb-temp-slug-7074000?preview=21d6760126a02464b0511498bbb95749150afb17a7ff6377c458ee54a8f57cfe00d4e258aa06bad3232c0dd9e73a2d62138fc990048987e9e2339a3d

I just registered a wiki account "sshymko" with the intention of submitting the 
RFC.
Could someone please approve the account and give it some karma?

Looking forward to collaborating with the internals team! 🙂

Regards,
Sergii Shymko

Reply via email to