您的位置:首页 > PHP > PHP源码阅读笔记十六:array_count_values函数

PHP源码阅读笔记十六:array_count_values函数

PHP源码阅读笔记十六:array_count_values
array_count_values

(PHP 4, PHP 5)
array_count_values — 统计数组中所有的值出现的次数
说明

array array_count_values ( array input )

array_count_values() 返回一个数组,该数组用 input 数组中的值作为键名,该值在 input 数组中出现的次数作为值。

源程序说明:
在源代码中的两句注释就说明了这个函数的实现

1
2
3
4
    /* Initialize return array */
    array_init(return_value);
 
    /* Go through input array and add values to the return array */

但是其中还有一些细节需要注意:
1、此函数只能识别字符串和数字,所以程序中使用了类似于下面的语句

1
2
3
4
5
if (Z_TYPE_PP(entry) == IS_LONG) {
} else if (Z_TYPE_PP(entry) == IS_STRING) {
} else {
            php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only count STRING and INTEGER values!");
}

2、在遍历过程中,首先判断是否不存在,此判断过程在针对字符串和数字时也有不同,但最终都是针对hash table的操作
在代码中针对zval的初始化使用的是宏zval *data; MAKE_STD_ZVAL(data);
跟踪此宏的定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MAKE_STD_ZVAL(data);
==> #define MAKE_STD_ZVAL(zv)                 \    zend.h 586行
    ALLOC_ZVAL(zv); \
    INIT_PZVAL(zv);
 
==> #define ALLOC_ZVAL(z)    \
    ZEND_FAST_ALLOC(z, zval, ZVAL_CACHE_LIST)    zend_alloc.h 165行
 
==> #define ZEND_FAST_ALLOC(p, type, fc_type)    \
    (p) = (type *) emalloc(sizeof(type))                   zend_alloc.h  152行
 
==> #define emalloc(size)                        _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)    zend_alloc.h 56行
==> ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)      zend_alloc.c 2288行 程序实现
 
==>  #define INIT_PZVAL(z)        \                 zend.h 576行
    (z)->refcount = 1;        \
    (z)->is_ref = 0;

EOF

本文地址:PHP源码阅读笔记十六:array_count_values函数    文章出处:PHP源码阅读,PHP设计模式,PHP学习笔记,项目管理-胖胖的空间

转载请以链接形式注明原始出处和作者,谢绝不尊重版权者抄袭!

暂无留言我要留言

必填

必填,绝不公开

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word