Skip to content

Add new flag ARRAY_FILTER_PACK_RESULT#22155

Open
DanielEScherzer wants to merge 1 commit into
php:masterfrom
DanielEScherzer:ARRAY_FILTER_PACK_RESULT
Open

Add new flag ARRAY_FILTER_PACK_RESULT#22155
DanielEScherzer wants to merge 1 commit into
php:masterfrom
DanielEScherzer:ARRAY_FILTER_PACK_RESULT

Conversation

@DanielEScherzer
Copy link
Copy Markdown
Member

No description provided.

@DanielEScherzer
Copy link
Copy Markdown
Member Author

Requested by someone at PHPTek and seemed pretty simple to implement
Our current documentation notes that

Array keys are preserved, and may result in gaps if the array was indexed. The result array can be reindexed using the array_values() function.

This would eliminate the need for an extra method call and improve performance

Plenty of existing code would use this: https://github.com/search?q=language%3Aphp%20-is%3Afork%20-is%3Aarchived%20%2F%5C%5C%3Farray_values%5C(%5Cs*%5C%5C%3Farray_filter%2F&type=code

let me know if an RFC is needed, not sure if this is small enough or not

@alecpl
Copy link
Copy Markdown

alecpl commented May 27, 2026

Excuse my ignorance. Isn't technicly [12 => 'bar', 42 => 'baz'] array a "packed array"? array_values() "returns an indexed array of values", as the docs say.

Wouldn't ARRAY_FILTER_REINDEX make more sense?

@jorgsowa
Copy link
Copy Markdown
Contributor

I was working on this topic in the past, and there are already some opinions about the proper direction. #18291

It would require discussion on the internals about the clear and consistent direction.

I would be happy for the list_* family functions for packed arrays.

@DanielEScherzer
Copy link
Copy Markdown
Member Author

DanielEScherzer commented May 27, 2026

Excuse my ignorance. Isn't technicly [12 => 'bar', 42 => 'baz'] array a "packed array"? array_values() "returns an indexed array of values", as the docs say.

Wouldn't ARRAY_FILTER_REINDEX make more sense?

A packed array is one with consecutive integer indexes, basically an array that would pass https://www.php.net/manual/en/function.array-is-list.php - see

php-src/Zend/zend_hash.h

Lines 1612 to 1615 in 9898293

if (HT_IS_PACKED(array)) {
if (HT_IS_WITHOUT_HOLES(array)) {
return 1;
}


@iluuu1994 @TimWolla you participated in the discussion on #18291 - thoughts on adding this as a new flag?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants