PHP_FUNCTION(array_chunk)
{
int argc = ZEND_NUM_ARGS(), key_type, num_in;
long size, current = 0;
char *str_key;
uint str_key_len;
zval *input = NULL;
zval *chunk = NULL;
zval **entry;
HashPosition pos;
if (zend_parse_parameters(argc TSRMLS_CC, "al|b", &input, &size, &preserve_keys) == FAILURE) { return;
}
/* Do bounds checking for size parameter. */
if (size < 1) {
return;
}
//获取数组元素个数
num_in = zend_hash_num_elements(Z_ARRVAL_P(input));
//如果分割的个数大于 数组的元素
if (size > num_in) {
最小为1
size = num_in > 0 ? num_in : 1;
}
//初始化返回值return_value
array_init_size(return_value, ((num_in - 1) / size) + 1);
//数组当前执行的元素指针
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void**)&entry, &pos) == SUCCESS) {
/* If new chunk, create and initialize it. */
if (!chunk) {
MAKE_STD_ZVAL(chunk);
array_init_size(chunk, size);
}
zval_add_ref(entry);
if (preserve_keys) { //如果保存原来的键值
//获取当前指针的key
key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(input), &str_key, &str_key_len, &num_key, 0, &pos);
switch (key_type) {
case HASH_KEY_IS_STRING://键值是字符串,将key和值添加到chunk里
add_assoc_zval_ex(chunk, str_key, str_key_len, *entry);
break;
default:
add_index_zval(chunk, num_key, *entry);
break;
}
} else {//重建数组索引 只将entry数据添加到chunki里
add_next_index_zval(chunk, *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);
chunk = NULL;
}
//移动到下一元素
zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos);
}
/* Add the final chunk if there is one. */
//添加到返回值return_value的Hash里
if (chunk) {
add_next_index_zval(return_value, chunk);
}
}
#include <array.au3>
Local $Arr[3006]
For $i=0 to UBound($Arr)-1;初始化数组
$Arr[$i]="SZ"&StringFormat('%06d',Random(1,999999,1))
Next
For $i=0 to UBound($Arr)-1 Step 100;是按100步进吗?
Local $str=''
For $n=$i to $i+100;步进为1
If $n> UBound($Arr)-1 Then ExitLoop;避免数组越界
$str&=$Arr[$n]&","
Next
$str=StringTrimRight($str,1)
MsgBox(0,0,$str)
Next