php内置函数分析array_count_values()
PHP_FUNCTION(array_count_values)
{
zval *input, /* Input array */
*entry, /* An entry in the input array */
*tmp;
HashTable *myht; if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &input) == FAILURE) {
return;
} /* Initialize return array */
array_init(return_value); /* Go through input array and add values to the return array */
myht = Z_ARRVAL_P(input);
// 循环遍历数组
ZEND_HASH_FOREACH_VAL(myht, entry) {
ZVAL_DEREF(entry);
/* 数组元素的值只能是字符串或整数 */
if (Z_TYPE_P(entry) == IS_LONG) { // 数组元素值为整数
if ((tmp = zend_hash_index_find(Z_ARRVAL_P(return_value), Z_LVAL_P(entry))) == NULL) { // 返回数组中不存在该键
zval data;
ZVAL_LONG(&data, ); // 首次出现,+1
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(entry), &data); // 添加到返回值数组
} else {
Z_LVAL_P(tmp)++; // 元素值出现次数+1
}
} else if (Z_TYPE_P(entry) == IS_STRING) { // 数组元素值为字符串
if ((tmp = zend_symtable_find(Z_ARRVAL_P(return_value), Z_STR_P(entry))) == NULL) {
zval data;
ZVAL_LONG(&data, );
zend_symtable_update(Z_ARRVAL_P(return_value), Z_STR_P(entry), &data);
} else {
Z_LVAL_P(tmp)++;
}
} else {
// 数组元素的值非字符串或整数,则报warning错误。
php_error_docref(NULL, E_WARNING, "Can only count STRING and INTEGER values!");
}
} ZEND_HASH_FOREACH_END();
}
php内置函数分析array_count_values()的更多相关文章
- map内置函数分析所得到的思路
map:会根据提供的函数对指定序列做映射. map(func, *iterables) --> map object Make an iterator that computes the fun ...
- php内置函数分析之array_diff_assoc()
static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_type) /* {{{ */ { uint ...
- php内置函数分析之array_combine()
PHP_FUNCTION(array_combine) { HashTable *values, *keys; uint32_t pos_values = ; zval *entry_keys, *e ...
- php内置函数分析之ucwords()
PHP_FUNCTION(ucwords) { zend_string *str; char *delims = " \t\r\n\f\v"; register char *r, ...
- php内置函数分析之strtoupper()、strtolower()
strtoupper(): PHP_FUNCTION(strtoupper) { zend_string *str; ZEND_PARSE_PARAMETERS_START(, ) Z_PARAM_S ...
- php内置函数分析之ucfirst()、lcfirst()
ucfirst($str) 将 str 的首字符(如果首字符是字母)转换为大写字母,并返回这个字符串. 源码位于 ext/standard/string.c /* {{{ php_ucfirst Up ...
- php内置函数分析之trim()
官方手册中: 类似函数还有两个:ltrim() 和 rtrim().分别处理字符串的左侧.右侧. trim()的具体实现位于:ext/standard/string.c /* {{{ proto st ...
- php内置函数分析之str_pad()
PHP_FUNCTION(str_pad) { /* Input arguments */ zend_string *input; /* Input string 输入字符串*/ zend_long ...
- php内置函数分析之array_fill_keys()
PHP_FUNCTION(array_fill_keys) { zval *keys, *val, *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), ...
随机推荐
- MySQL表碎片清理
MySQL大表清理 生产环境data库业务表base_data大小:500G,data_free:31G mysql> SELECT table_schema,table_name,data_f ...
- python使用内置方法和修饰器方法获取类名、函数名
1. 外部获取 从外部的情况好获取,可以使用指向函数的对象,然后用__name__属性. def a(): pass a.__name__ 或者 getattr(a,'__name__') 2. 内部 ...
- 阶段3 1.Mybatis_11.Mybatis的缓存_1 今日课程安排
- WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
我们知道,在Spring Boot 2.0后用自己的的配置类继承WebMvcConfigurerAdapter时,idea会提示这个类已经过时了. 通常情况下我们会采用下面两种代替方案: 实现WebM ...
- SpringContextHolder使用报错
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dobecoder/article/details/88401612今天在使用SpringContex ...
- 各种CNN模型
Resnet: model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'res ...
- 爬取LeetCode题目——如何发送GraphQL Query获取数据
前言 GraphQL 是一种用于 API 的查询语言,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得 ...
- kafka学习(六)
用kafka构建数据管道 把kafka看着是一个数据的端点,怎么把kafka数据移到mysql,elasticSearchs 这里面介绍kafka connect API怎么样帮忙我们把数据移到我 ...
- TestNG+extentReports+log4j2 完善自动化测试框架——美观的报告和保留日志文件
1:导入Maven依赖<dependency> <groupId>com.aventstack</groupId> <artifactId>extent ...
- redis配置主从出现DENIED Redis is running in protected mode
修改redis配置文件,将绑定的ip给注释掉 #127.0.0.1 在配置文件中将protected-mode 改为no protected-mode no 另一种方式是在配置文件中设置密码 requ ...