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. maven 成长之路

    1配置maven 环境变量 新建系统变量 M2_HOME :E:\apache-maven-3.5.2 在系统变量 path中添加 E:\apache-maven-3.5.2\bin 运行 mvn - ...

  2. windows程序调试

    由于不能在控制台输出,可以使用Messagebox 但是有时候要用到输出int之类的,需要转换.转换过程中有会有很多问题. 这里给出两个可行的代码 int a = 5, b = 10; int res ...

  3. 【mysql】如何通过navicat配置表与表的多对一关系,一对一关系?设计外键的效果

    背景: 现在要将接口自动化测试结果持久化,当前只是每次运行接口测试,将测试结果通过邮件发送给项目组成员.邮件内容如下: 表设计: 为了呈现这个结果:我设计了2张表run_result和run_deta ...

  4. VS调试异常代码 HRESULT:0x80070057 (E_INVALIDARG)解决方法

    我目前在做的一个系统是VS2010写的的B/S架构程序, 主要技术是:C#.SQLSERVER2008.NHibernate,Python,Nhibernate 的*.hbn.xml是映射数据库的表结 ...

  5. java网络通信:同步阻塞式I/O模型(BIO)

    缺点:一个线程只能处理一个客户端连接 服务端: public class TimeServer { public static void main(String[] args) throws IOEx ...

  6. Linux监控命令之==>sar

    一.使用说明 sar 是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情况.磁盘I/O.CPU效率.内存使用状况.进程活动及 ...

  7. LoadRunner之关联

    一.什么是关联 关联就是将服务器动态返回变化的值保存为一个参数以供后面需要用到的地方使用. 二.什么时候需要关联 1.服务器返回中存在动态变化的值,一般是类似session.token这样的无规则数据 ...

  8. CMakeLists.txt中使用循环

    编译一个安卓下的so,此so依赖其他的库,通过循环简化操作 set(UVC_LIBS UVCCamera uvc usb100 jpeg-turbo1500) FOREACH(UVC_LIB ${UV ...

  9. robot framework python3环境下学习笔记(1)——安装robot framework

    安装环境:win10 64位,python3.6 1,安装robot framework pip install robotframework 2,安装wxPython pip install wxP ...

  10. bzoj3028食物 关于(1+x+x2+x3+x4+...)^k的第i项系数就是c(i+k−1,k−1)的证明

    关于(1+x+x2+x3+x4+...)^k的第i项系数就是c(i+k−1,k−1)的证明对于第i项,假设为5x^5=x^0*x^5x^5=x^1*x^4x^5=x^2*x^3........也就是说 ...