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. JavaScript json loop item in array

    Iterating through/Parsing JSON Object via JavaScript 解答1 Your JSON object is incorrect because it ha ...

  2. 【C++进阶】 to_string,stringstream

    to_string函数主要进行以下一些参数转换为string stringstream,位于<sstream>库中 https://blog.csdn.net/jllongbell/art ...

  3. fiddler之数据分析和查看(inspectors)-抓包

    在instpectors中主要是对请求和响应进行查看和分享,监听请求的响应内容.他有多个分页标签.界面分上下两部分,上面部分显示请求的相关信息:下面部分显示响应相关信息.简单说明常用的几个分页标签 一 ...

  4. Linux下编译安装Python-3.6.5

    1.下载Python-3.6.5安装包 在Python官网(https://www.python.org/downloads/)下载对应的安装包,选择3.6.5的linux版本,如下图: 2.将安装包 ...

  5. Scala的集合框架

    1.元组 定义方式:val tp=("nana',1,1.1) 特点:集合中的数据可以是不同类型的 最多只能放22个元素 取值:通过角标取值,这里的角标是从1开始的,元组名称._角标   t ...

  6. 批处理文件将多台连接的手机安装同一个APP

    :adb devices > C:\Users\aaminaxu\Desktop\a.txt :1.首先获取连接到电脑的手机的设备信息将其保存到C盘的a.txt文件中::2.将保存的txt文件中 ...

  7. 【Linux开发】【Qt开发】Qt界面键盘、触摸屏、鼠标的响应设置

    USB键盘 经过一番搜索,发现对Qt键盘的支持主要关系到两个方面: 1. 键盘类型确定: 4.7以前的Qt版本,如果是PS2圆孔键盘,Qt编译时需加上选项:-qt-kbd-vr41xx(未测试):如果 ...

  8. Canvas入门08-绘制仪表盘

    需求 实现下图所示的仪表盘的绘制. 分析 我们先来将仪表盘进行图形拆分,并定义尺寸. 我们绘制的逻辑: 绘制中心圆 绘制环外圈圆 绘制环内圈圆 绘制刻度内圈圆 绘制刻度线 绘制刻度文字 绘制指针 定义 ...

  9. swagger 访问格式

    boot工程格式如下 http://10.166.10.169:8085/swagger-ui.html 非boot工程加个自己项目名 http://10.166.10.169:8085/xxx/sw ...

  10. mysql先分组,然后取每个分组中的第2大的记录

    文章参考http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ 首先建表: ...