PHP_FUNCTION(array_chunk)
{
int argc = ZEND_NUM_ARGS(), num_in;
zend_long size, current = ;
zend_string *str_key;
zend_ulong num_key;
zend_bool preserve_keys = ;
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. */
/* 如果 size 小于 1,会抛出一个 E_WARNING 错误并返回 NULL。 */
if (size < ) {
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)); /* 1 <= size <= num_in */
if (size > num_in) {
size = num_in > ? num_in : ;
} /* 初始化返回值 ((num_in - 1) / size) + 1)个元素 */
array_init_size(return_value, (uint32_t)(((num_in - ) / size) + )); // 设置(zval).u1.type_info = IS_UNDEF
// chunk保存分块后的每个数组
ZVAL_UNDEF(&chunk); ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input), num_key, str_key, entry) {
/* If new chunk, create and initialize it. */
// 类型位IS_UNDEF表明,这个chunk是一个新的分块
if (Z_TYPE(chunk) == IS_UNDEF) {
// 初始化chunk数组,大小为size
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 { // 不保存原数组中的键名,重新索引
// 数组元素插入chunk
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. */
/*chunk大小达到size之后,将chunk加入到返回值数组return_value中,也就是一个分块完成。
* 然后重置chunk为IS_UNDEF。
*/
if (!(++current % size)) {
// chunk加入到return_value
add_next_index_zval(return_value, &chunk);
// 重置chunk
ZVAL_UNDEF(&chunk);
}
} ZEND_HASH_FOREACH_END(); /* Add the final chunk if there is one. */
/* 最后一个数组分块大小达不到size的时候, 将其加到return_value */
if (Z_TYPE(chunk) != IS_UNDEF) {
add_next_index_zval(return_value, &chunk);
}
}

php内置函数分析之array_chunk()的更多相关文章

  1. map内置函数分析所得到的思路

    map:会根据提供的函数对指定序列做映射. map(func, *iterables) --> map object Make an iterator that computes the fun ...

  2. php内置函数分析之array_diff_assoc()

    static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_type) /* {{{ */ { uint ...

  3. php内置函数分析之array_combine()

    PHP_FUNCTION(array_combine) { HashTable *values, *keys; uint32_t pos_values = ; zval *entry_keys, *e ...

  4. php内置函数分析之ucwords()

    PHP_FUNCTION(ucwords) { zend_string *str; char *delims = " \t\r\n\f\v"; register char *r, ...

  5. php内置函数分析之strtoupper()、strtolower()

    strtoupper(): PHP_FUNCTION(strtoupper) { zend_string *str; ZEND_PARSE_PARAMETERS_START(, ) Z_PARAM_S ...

  6. php内置函数分析之ucfirst()、lcfirst()

    ucfirst($str) 将 str 的首字符(如果首字符是字母)转换为大写字母,并返回这个字符串. 源码位于 ext/standard/string.c /* {{{ php_ucfirst Up ...

  7. php内置函数分析之trim()

    官方手册中: 类似函数还有两个:ltrim() 和 rtrim().分别处理字符串的左侧.右侧. trim()的具体实现位于:ext/standard/string.c /* {{{ proto st ...

  8. php内置函数分析之str_pad()

    PHP_FUNCTION(str_pad) { /* Input arguments */ zend_string *input; /* Input string 输入字符串*/ zend_long ...

  9. php内置函数分析之array_fill_keys()

    PHP_FUNCTION(array_fill_keys) { zval *keys, *val, *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), ...

随机推荐

  1. 接入集团auth流程

    前言 一直对集团的auth系统很感兴趣,所以这次记录下接入集团auth的流程.如果后期有时间,会补充具体的auth实现细节. 正文 一.实现思想 1. 实现思想 明确几个名词:接入方,管理方.接入方指 ...

  2. Latex常用公式整理

    目录 常用 常用数学公式 常用希腊字母 说明:博客园中的Latex编辑是以$ latex公式 $,为边界. 1.常用 描述 Latex公式 表达式 下标 x_2 x2 上标 x^2 x2  分数 \f ...

  3. 构造Map并对其排序

    #构造Map并对其排序 attr_tul = ['a','b','c','d','e','f'] one_tul = [,,,,,] one_dic = {} for i in range(len(a ...

  4. leetcode 27. 移除元素(python)

    1. 题目描述 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外 ...

  5. Stream之filter、distinct、skip、map、flatMap、match、find、reduce

    一.Stream之filter.distinct.skip: package com.cy.java8; import java.util.Arrays; import java.util.List; ...

  6. gitlab+jenkins 搭建

    继前一篇gitlab,这一篇介绍jenkins搭建并与gitlab进行集成---这里不是详细的步骤 环境系统:centos 7.3 jenkins版本:jenkins-2.176.1-1.1.noar ...

  7. ReportManager

    package com.neusoft.report.engine; import com.neusoft.report.common.Logger; import com.neusoft.repor ...

  8. 正则表达式断言(Assertions)

    一 零宽正向先行断言 x(?=y) 仅匹配被y跟随的x. const regExp = /Jack(?=Sparrow|Dawson)/g; const str = 'JackJones JackSp ...

  9. 嵌套的frame

    自动化的测试中,iframe的嵌套也是很常见的,对于嵌套的iframe,我们处理的方式是先进入到iframe的父节点, 再进入到子节点,然后可以对子节点里面的对象进行处理和操作.如下的html代码效果 ...

  10. 【EW系列】SAP EWM模块-EWM的常用T-CODE整理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[EWM系列]SAP EWM模块-EWM的常用T ...