2014-09-05
typedef int (*apply_func_t)(void *pDest TSRMLS_DC);void zend_hash_apply(HashTable *ht, apply_func_t apply_func TSRMLS_DC);
typedef int (*apply_func_arg_t)(void *pDest, void *argument TSRMLS_DC);void zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *data TSRMLS_DC);一个是可以传递参数的,还有一个是不传递参数,只传递哈希表中的值的。对于可传递参数的函数而言,在扩展中应用到可能性更大。回调函数可能有不同的返回值:
int php_sample_print_zval(zval **val TSRMLS_DC){ //重新copy一个zval,防止破坏原数据 zval tmpcopy = **val; zval_copy_ctor(&tmpcopy); //转换为字符串 INIT_PZVAL(&tmpcopy); convert_to_string(&tmpcopy); //开始输出 php_printf("The value is: "); PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy)); php_printf("/n"); //毁尸灭迹 zval_dtor(&tmpcopy); //返回,继续遍历下一个~ return ZEND_HASH_APPLY_KEEP;}然后定义循环函数:zend_hash_apply(arrht, php_sample_print_zval TSRMLS_CC);
$val) { echo "The value of $key is: $val/n"; }?>针对这段php代码的c代码:
int php_sample_print_zval_and_key(zval **val, int num_args, va_list args, zend_hash_key *hash_key) { /* 复制zval从而使得原来的内容被保存下来 */ zval tmpcopy = **val; /* tsrm_ls is needed by output functions */ TSRMLS_FETCH(); zval_copy_ctor(&tmpcopy); /* Reset refcount & Convert */ INIT_PZVAL(&tmpcopy); convert_to_string(&tmpcopy); /* 输出 */ php_printf("The value of "); if (hash_key->nKeyLength) { /* 如果是字符串类型的key */ PHPWRITE(hash_key->arKey, hash_key->nKeyLength); } else { /* 如果是数字类型的key */ php_printf("%ld", hash_key->h); } php_printf(" is: "); PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy)); php_printf("/n"); /* Toss out old copy */ zval_dtor(&tmpcopy); /* continue; */ return ZEND_HASH_APPLY_KEEP;}
1, 'b'=>2, 'c'=>3); reset($arr); while (list($key, $val) = each($arr)) { /* Do something with $key and $val */ } reset($arr); $firstkey = key($arr); $firstval = current($arr); $bval = next($arr); $cval = next($arr);?>每一个相应的函数都会有一个zend版本:
* /* reset() */void zend_hash_internal_pointer_reset(HashTable *ht); /* key() */int zend_hash_get_current_key(HashTable *ht, char **strIdx, unit *strIdxLen, ulong *numIdx, zend_bool duplicate); * /* current() */int zend_hash_get_current_data(HashTable *ht, void **pData); * /* next()/each() */int zend_hash_move_forward(HashTable *ht); * /* prev() */int zend_hash_move_backwards(HashTable *ht); * /* end() */void zend_hash_internal_pointer_end(HashTable *ht); * /* Other... */int zend_hash_get_current_key_type(HashTable *ht);int zend_hash_has_more_elements(HashTable *ht);next() prev() end()其实都是找到相应的索引值,再用zend_hash_get_current_data()返回元素值
void php_sample_print_var_hash(HashTable *arrht){ for(zend_hash_internal_pointer_reset(arrht); zend_hash_has_more_elements(arrht) == SUCCESS; zend_hash_move_forward(arrht)) { char *key; uint keylen; ulong idx; int type; zval **ppzval, tmpcopy; type = zend_hash_get_current_key_ex(arrht, &key, &keylen, &idx, 0, NULL);//获得返回的key的类型。这个类型可能有三种 if (zend_hash_get_current_data(arrht, (void**)&ppzval) == FAILURE) {//获得当前索引所指的数据值 /* Should never actually fail * since the key is known to exist. */ continue; } /* 复制zval的值,从而原来的值不会被破坏掉 */ tmpcopy = **ppzval; zval_copy_ctor(&tmpcopy); /* 重新设定refcount 并且转换 */ INIT_PZVAL(&tmpcopy); convert_to_string(&tmpcopy); /* 输出 */ php_printf("The value of "); if (type == HASH_KEY_IS_STRING) { /* String Key / Associative */ PHPWRITE(key, keylen); } else { /* Numeric Key */ php_printf("%ld", idx); } php_printf(" is: "); PHPWRITE(Z_STRVAL(tmpcopy), Z_STRLEN(tmpcopy)); php_printf("/n"); /* 销毁原来的副本 */ zval_dtor(&tmpcopy); }}来看一下zend_hash_get_current_key_ex返回值的可能性:Constant Meaning
int zend_hash_del(HashTable *ht, char *arKey, uint nKeyLen);int zend_hash_index_del(HashTable *ht, ulong h);返回SUCCESS OR FAILURE
int sample_strvec_handler(int argc, char **argv TSRMLS_DC){ HashTable *ht; /* 为哈希表分配空间 */ ALLOC_HASHTABLE(ht); /* 初始化哈希表的内部状态 */ if (zend_hash_init(ht, argc, NULL, ZVAL_PTR_DTOR, 0) == FAILURE) { FREE_HASHTABLE(ht); return FAILURE; } /* 把每个字符串变成zval* */ while (argc) { zval *value; MAKE_STD_ZVAL(value); ZVAL_STRING(value, argv[argc], 1); argv++; if (zend_hash_next_index_insert(ht, (void**)&value, sizeof(zval*)) == FAILURE) { /* 对于分配失败的情况应该跳掉 */ zval_ptr_dtor(&value); } } /* Do some work */ process_hashtable(ht); /* 毁坏哈希表 * 释放所有的分配的空旷 */ zend_hash_destroy(ht); /* Free the HashTable itself */ FREE_HASHTABLE(ht); return SUCCESS;}
int zend_hash_minmax(HashTable *ht, compare_func_t compar, int flag, void **pData TSRMLS_DC);flag为0就返回最小值,否则就是最大值。
int fname_compare(zend_function *a, zend_function *b TSRMLS_DC){ return strcasecmp(a->common.function_name, b->common.function_name);}void php_sample_funcname_sort(TSRMLS_D){ zend_function *fe; if (zend_hash_minmax(EG(function_table), fname_compare, 0, (void **)&fe) == SUCCESS) { php_printf("Min function: %s/n", fe->common.function_name); } if (zend_hash_minmax(EG(function_table), fname_compare, 1, (void **)&fe) == SUCCESS) { php_printf("Max function: %s/n", fe->common.function_name); }}
typedef void (*sort_func_t)(void **Buckets, size_t numBuckets, size_t sizBucket, compare_func_t comp TSRMLS_DC);int zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber TSRMLS_DC);一般就用zend_qsort作为sort_func就够了。renumber这个参数如果设为1的话,那么就会抛弃原有的索引键值关系,赋予新的数字键值。zend_hash_sort(target_hash, zend_qsort,array_data_compare, 1 TSRMLS_CC);array_data_compare是一个返回compare_func_t类型数据的函数,它将按照HashTable中zval*值的大小进行排序。
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
6
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
7
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
8
常用dos命令及语法
09-27
PHP中include和require区别之我见
2014-09-05
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
java中的info是什么意思
2022-03-24
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
学ug编程如何快速入门?
2022-03-17
PHP数组函数array
2014-09-05
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
道友请留步正版手游下载v7.10.090301 安卓版
卡牌对战 212.0M
下载武林盛典手游下载v9991.1 安卓版
角色扮演 147.7M
下载推理学院手游下载v5.64 安卓最新版本
其它手游 207.4M
下载喵星大作战最新版本下载v3.22 安卓官方版
其它手游 131.8M
下载保卫碎碎岛游戏下载v10.20 安卓版
策略塔防 80.6M
下载僵尸军团手机版下载v2.3 安卓中文版
其它手游 76.7M
下载坦克连竞技版最新版下载v1.4.1 安卓版
策略塔防 1.83G
下载疾风暴打手游正版下载v2.0 安卓版
其它手游 82.1M
下载果盘巅峰坦克手游下载v6.5.0 安卓最新版
下载
九游巅峰坦克官方版下载v6.5.0安卓版
下载
巅峰坦克装甲战歌游戏下载v6.5.0 安卓版
下载
森之灵plop saga下载v1.5 安卓版
下载
蓝方战地模拟器游戏下载v1.4.1 安卓版
下载
纸上帝国小游戏下载v1.2 安卓版
下载
纸上帝国h5小游戏下载v1.2 安卓版
下载
枪手丧尸幸存者安卓版下载v7.1.0 安卓版
下载