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. lr_save_string和sprintf、lr_eval_string的使用

    一.lr_save_string函数 1.该函数主要是将程序中的常量或变量保存为参数: //将常量保存为参数 lr_save_string("777","page&quo ...

  2. easyhook源码分析二——注入

    EasyHook 中的注入方法. 函数原型 // EasyHook 中的命名比较有意思,Rh 代表的就是Remote Hook,此函数就是远程钩子的一个子过程----注入,前面的宏代表它是导出函数. ...

  3. kaptcha Spring 整合

    转自:http://my.oschina.net/CandyDesire/blog/209364 生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的 ...

  4. Unity 实现物体拖拽

    Unity实现拖拽: 也可以继承Unity EventSystem中的接口实现. 当鼠标按下的时候以左键为例: Using System.Collections; Using System.Colle ...

  5. ActionList及Action使用

    ActionList及Action使用 https://blog.csdn.net/adamrao/article/details/7450889 2012年04月11日 19:09:27 阅读数:1 ...

  6. MinGW GCC 9.1 2019年5月3日 出炉啦

    GNU 2019-05-03 发布了 GCC 9.1 https://gcc.gnu.org/onlinedocs/9.1.0/ 有详细的说明MinGW 上可用的 GCC 9.1 版本下载地址 [ m ...

  7. Golang http post error : http: ContentLength=355 with Body length 0

    参考:https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0 问 ...

  8. centos7安装java JDK

    Java环境 1.下载jdk(用FileZilla或xshell工具连接服务器后上传到需要安装的目录) 在 /opt/deploy 下新建 java 文件夹: # mkdir / opt/deploy ...

  9. Java ——补充:构造方法 super()与构造方法 无参 有参构造方法 this()与构造方法

    参考文章: https://blog.csdn.net/qq_33322074/article/details/86030836 https://blog.csdn.net/HD243608836/a ...

  10. C++ Primer笔记(1)——连续读取数据、类型对应的尺寸、类型转换、字符串分行写法

    这次要看看C++ Primer,这本基本上就是必读书籍了.下面的内容就是一些之前没有学过的知识的笔记. 读取数量不定的输入数据 虽然很简单,但是还是记一下: #include <iostream ...