In short, you can zaminovatov, for what I publish a refutation of the answer @Mike in support of @ Ipatiev ...
But still I will do it ...
and so consider the array_chunk function
/* {{{ proto array array_chunk(array input, int size [, bool preserve_keys]) Split array into chunks */ PHP_FUNCTION(array_chunk) { int argc = ZEND_NUM_ARGS(), num_in; zend_long size, current = 0; zend_string *str_key; zend_ulong num_key; zend_bool preserve_keys = 0; zval *input = NULL; zval chunk; zval *entry; if (zend_parse_parameters(argc, "al|b", &input, &size, &preserve_keys) == FAILURE) { return; } /* Do bounds checking for size parameter. */ if (size < 1) { php_error_docref(NULL, E_WARNING, "Size parameter expected to be greater than 0"); return; } num_in = zend_hash_num_elements(Z_ARRVAL_P(input)); if (size > num_in) { size = num_in > 0 ? num_in : 1; } array_init_size(return_value, (uint32_t)(((num_in - 1) / size) + 1)); 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) { array_init_size(&chunk, (uint32_t)size); } /* Add entry to the chunk, preserving keys if necessary. */ if (preserve_keys) { if (str_key) { entry = zend_hash_update(Z_ARRVAL(chunk), str_key, entry); } else { entry = zend_hash_index_update(Z_ARRVAL(chunk), num_key, entry); } } else { entry = zend_hash_next_index_insert(Z_ARRVAL(chunk), entry); } 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); } } 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); } }
And so pay attention to the line
/* 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); }
What does she tell us? the same is correct as in the answer we calculate the remainder of the division by this same size and add the resulting chunk return_value; note the chunk is the link ie physically, this is one variable. While this code is more optimized as it is:
Compiled
It is typed (there is no type conversion, etc., etc.)
A hash of the size from the most needed length is created without a stock (for older versions of php there may be another implementation)
those. in fact, we see that the answer from the T-shirt is a little less than the full functionality of array_chunk and even in speed and in memory consumption, they will be comparable, in certain relative planes.
ps @Mike so I see no reason to write 'not nice' for the sake of dubious optimization.
source code: https://github.com/php/php-src/blob/bd9858ead26f2b1537ca61f3f5a136f8ec160a27/ext/standard/array.c
pps Well, a little argument for coding stait
simple example
if($num % 3 == 2) echo '</div>'; $num++;
no mistakes, all is well.
if($num % 3 == 2) // echo '</div>'; $num++;
There are no errors, but the algorithm does not work.
more common example
// тут сотня строк подобного кода if($b == 2) $a = 2; $a=1; // ну и тут так же
good example seen in coding style for si writing conditions
if(2 == $a) {}
cool yes sealed like this if(2=$a) and you got a mistake. and if we examine how we write
if($a == 2) {}
and if if($a=2) sealed, then $a is equal to 2, there is no error, the algorithm is not working.
pps I still think that it is necessary to write beautifully and what would work, while in the community where they help, so that the junior would not ask the Temlid, а где ты вот этот кусочек откапал кода? did а где ты вот этот кусочек откапал кода?