Skip to content

Commit b9562ad

Browse files
authored
standard: Simplify array_chunk() (#20423)
1 parent d8ac527 commit b9562ad

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

ext/standard/array.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7037,21 +7037,19 @@ PHP_FUNCTION(array_chunk)
70377037

70387038
if (size > num_in) {
70397039
if (num_in == 0) {
7040-
RETVAL_EMPTY_ARRAY();
7041-
return;
7040+
RETURN_EMPTY_ARRAY();
70427041
}
70437042
size = num_in;
70447043
}
70457044

70467045
array_init_size(return_value, (uint32_t)(((num_in - 1) / size) + 1));
70477046
zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
70487047

7049-
ZVAL_UNDEF(&chunk);
7050-
70517048
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input), num_key, str_key, entry) {
70527049
/* If new chunk, create and initialize it. */
7053-
if (Z_TYPE(chunk) == IS_UNDEF) {
7050+
if (current == 0) {
70547051
array_init_size(&chunk, (uint32_t)size);
7052+
add_next_index_zval(return_value, &chunk);
70557053
}
70567054

70577055
/* Add entry to the chunk, preserving keys if necessary. */
@@ -7066,19 +7064,10 @@ PHP_FUNCTION(array_chunk)
70667064
}
70677065
zval_add_ref(entry);
70687066

7069-
/* If reached the chunk size, add it to the result array, and reset the
7070-
* pointer. */
70717067
if (++current == size) {
7072-
add_next_index_zval(return_value, &chunk);
7073-
ZVAL_UNDEF(&chunk);
70747068
current = 0;
70757069
}
70767070
} ZEND_HASH_FOREACH_END();
7077-
7078-
/* Add the final chunk if there is one. */
7079-
if (Z_TYPE(chunk) != IS_UNDEF) {
7080-
add_next_index_zval(return_value, &chunk);
7081-
}
70827071
}
70837072
/* }}} */
70847073

0 commit comments

Comments
 (0)