1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

standard: Simplify array_chunk() (#20423)

This commit is contained in:
Niels Dossche
2025-11-12 17:20:41 +01:00
committed by GitHub
parent d8ac527c64
commit b9562adff6

View File

@@ -7037,8 +7037,7 @@ PHP_FUNCTION(array_chunk)
if (size > num_in) {
if (num_in == 0) {
RETVAL_EMPTY_ARRAY();
return;
RETURN_EMPTY_ARRAY();
}
size = num_in;
}
@@ -7046,12 +7045,11 @@ PHP_FUNCTION(array_chunk)
array_init_size(return_value, (uint32_t)(((num_in - 1) / size) + 1));
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
ZVAL_UNDEF(&chunk);
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input), num_key, str_key, entry) {
/* If new chunk, create and initialize it. */
if (Z_TYPE(chunk) == IS_UNDEF) {
if (current == 0) {
array_init_size(&chunk, (uint32_t)size);
add_next_index_zval(return_value, &chunk);
}
/* Add entry to the chunk, preserving keys if necessary. */
@@ -7066,19 +7064,10 @@ PHP_FUNCTION(array_chunk)
}
zval_add_ref(entry);
/* If reached the chunk size, add it to the result array, and reset the
* pointer. */
if (++current == size) {
add_next_index_zval(return_value, &chunk);
ZVAL_UNDEF(&chunk);
current = 0;
}
} ZEND_HASH_FOREACH_END();
/* Add the final chunk if there is one. */
if (Z_TYPE(chunk) != IS_UNDEF) {
add_next_index_zval(return_value, &chunk);
}
}
/* }}} */