PHP_FUNCTION(array_column)
{
zval *zcolumn = NULL, *zkey = NULL, *data;
HashTable *arr_hash;
zval *zcolval = NULL, *zkeyval = NULL, rvc, rvk; // !如果接收了一个php语言里的null变量,则直接将其转成C语言里的NULL,而不是封装成IS_NULL类型的zval
if (zend_parse_parameters(ZEND_NUM_ARGS(), "hz!|z!", &arr_hash, &zcolumn, &zkey) == FAILURE) {
return;
} /* 检查colunm和index是否有效(只能为数字和对象,对象需能转换为字符串)。
* 无效则报The %s key should be either a string or an integer的warning错误。
*/
if ((zcolumn && !array_column_param_helper(zcolumn, "column")) ||
(zkey && !array_column_param_helper(zkey, "index"))) {
RETURN_FALSE;
}
// 初始化返回值
array_init(return_value);
// 循环遍历数组
ZEND_HASH_FOREACH_VAL(arr_hash, data) {
ZVAL_DEREF(data); if (!zcolumn) { // column未设置
zcolval = data;
} else if ((zcolval = array_column_fetch_prop(data, zcolumn, &rvc)) == NULL) {// 该列的值为NULL,则跳过,进入下一循环
continue;
} /* Failure will leave zkeyval alone which will land us on the final else block below
* which is to append the value as next_index
*/
if (zkey) {
zkeyval = array_column_fetch_prop(data, zkey, &rvk); // 获取zkey对应的列,作为返回数组的键/索引
} Z_TRY_ADDREF_P(zcolval);
// 返回值数组:array(zkeyval=>zcolval,...)
if (zkeyval && Z_TYPE_P(zkeyval) == IS_STRING) {
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(zkeyval), zcolval);
} else if (zkeyval && Z_TYPE_P(zkeyval) == IS_LONG) {
add_index_zval(return_value, Z_LVAL_P(zkeyval), zcolval);
} else if (zkeyval && Z_TYPE_P(zkeyval) == IS_OBJECT) {
zend_string *key = zval_get_string(zkeyval);
zend_symtable_update(Z_ARRVAL_P(return_value), key, zcolval);
zend_string_release(key);
} else {
// 没有zkeyval,则返回值数组为索引数组
add_next_index_zval(return_value, zcolval);
}
/* 清理内存 */
if (zcolval == &rvc) {
zval_ptr_dtor(&rvc);
}
if (zkeyval == &rvk) {
zval_ptr_dtor(&rvk);
}
} ZEND_HASH_FOREACH_END();
}

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

  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. ES6数组内对象去重

    这个数组去重转自https://www.cnblogs.com/caideyipi/p/7679681.html, 就当笔记记录: 去重Set const arr = ['张三','张三','三张三' ...

  2. Array Stack Implement using C

  3. trim配合prefix,prefixOverrides,suffix,suffixOverrides构建动态sql语句

    1.在接口构建方法 public interface EmployeeMapperDynamicSQL { //携带了哪个字段查询条件就带上这个字段的值 public List<Employee ...

  4. 终于, Delphi XE2 携带 GDI+ 库了

    终于, Delphi XE2 携带 GDI+ 库了 使用了较早的 http://www.progdigy.com uses Winapi.GDIPAPI, Winapi.GDIPOBJ{, Winap ...

  5. 开发一个Flink应用

    步骤列表本次实战经历以下步骤: 创建应用:编码:构建:提交任务到Flink,验证功能: 环境信息Flink:1.7:Flink所在机器的操作系统:CentOS Linux release 7.5.18 ...

  6. 【Linux 应用编程】进程管理 - 进程间通信IPC之共享内存 mmap

    IPC(InterProcess Communication,进程间通信)是进程中的重要概念.Linux 进程之间常用的通信方式有: 文件:简单,低效,需要代码控制同步 管道:使用简单,默认阻塞 匿名 ...

  7. LeetCode.868-二进制距离(Binary Gap)

    这是悦乐书的第333次更新,第357篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868).给定正整数N,找到并返回N的二进制表示中两个连续1之间的最 ...

  8. Discrete Mathematics and Its Applications | 1 CHAPTER The Foundations: Logic and Proofs | 1.3 Propositional Equivalences

    DEFINITION 1 A compound proposition that is always true,no matter what the truth values of the propo ...

  9. 只需要2个工具,百度云盘大文件就能用迅雷和IDM下载

    不会代码,不懂脚本,没关系 ,能找到一座通往它们的桥梁,照样能到达彼岸. 这里以360极速浏览器为例. 在浏览器地址框输入以下地址直接到达浏览器安装扩展插件的地方(偷个懒,复制网址吧),https:/ ...

  10. [Web 前端] 030 ajax 是什么

    AJAX 是什么 1. AJAX 是一种"艺术" 简单地说 AJAX 是在不重新加载整个页面的情况下与服务器交换数据并更新部分网页的艺术 网上是这样说的 AJAX 指异步 Java ...