[php-src] PR #25908: ext/standard: Add array_flatten()
| From: | otar | Date: | Sun, 29 Nov 2026 21:43:39 +0000 |
| Subject: | [php-src] PR #25908: ext/standard: Add array_flatten() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-41217@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25908
Author: otar
array_flatten(array $array, int $depth = PHP_INT_MAX): array returns the values of nested arrays as
a list. It discards all keys. The $depth argument limits how many nesting levels the function
flattens. By default, there is no limit.
The function walks the input two times. The first pass counts the values and checks the stack limit,
recursive arrays, and the HT_MAX_SIZE limit. Then the function allocates one packed array with the
exact size, and the second pass copies the values. If there is nothing to flatten, the result is the
same as from array_values().
The function follows the rules of existing array functions:
- A reference with a refcount of more than 1 stays a reference, as in array_values().
- If the function must flatten an array that contains itself, it throws an Error "Recursion
detected", as array_walk_recursive() does.
- A negative depth throws a ValueError.
ext/gd/tests/bug47946.phpt declared its own global array_flatten(). The test now uses the new
function.