目录
- String 的两个宏定义
- 初始化数组 array_init
- 添加数组数据 add_assoc_null
- 获取数组中HashTable
- 根据 key 获取数组的 value
- 创建一个HashTable
- 获取HashTable元素数量
- 获取HashTable元素的最小/最大值
- HashTable遍历操作
- 获取指定HashPosition位置处的值
- HashTable数据更新
- HashTable的销毁
- HashTable 遍历修改操作示例
String 的两个宏定义
在Zend 内核中,针对字符串,定义了两个宏, 这个在后续的字符串处理中经常需要用到,大家留意
----zend_portability.h----
#define ZEND_STRL(str) (str), (sizeof(str)-1)
#define ZEND_STRS(str) (str), (sizeof(str))
初始化数组
int array_init(zval *arg);
在初始化数组之前, 你需要首先为 zval 分配内存。如下示例:
----php7_wrapper.h----
#if PHP_MAJOR_VERSION < 7 /* PHP Version 5*/
#define SW_MAKE_STD_ZVAL(p) MAKE_STD_ZVAL(p)
#define SW_ALLOC_INIT_ZVAL(p) ALLOC_INIT_ZVAL(p)
#define sw_zval_ptr_dtor(p) zval_ptr_dtor(*p) //zval销毁
#else /* PHP Version 7 */
//栈上分配空间
#define SW_MAKE_STD_ZVAL(p) zval _stack_zval_##p; p = &(_stack_zval_##p)
#define SW_ALLOC_INIT_ZVAL(p) do{p = (zval *)emalloc(sizeof(zval)); bzero(p, sizeof(zval));}while(0)
#define sw_zval_ptr_dtor(p) zval_ptr_dtor(*p) //zval销毁
#endif
----swoole_server.c----
static PHP_METHOD(swoole_http_client, __construct)
{
...
//SW_MAKE_STD_ZVAL分配的zval内存必须在使用后手动释放
zval *headers;
SW_MAKE_STD_ZVAL(headers);
array_init(headers); //初始化为数组
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("headers"), headers TSRMLS_CC);
sw_zval_ptr_dtor(&headers);
...
//SW_ALLOC_INIT_ZVAL 分配的 zval 内存由 php 回收
zval *ports;
SW_ALLOC_INIT_ZVAL(ports);
array_init(ports); //初始化为数组
zend_update_property(swoole_http_client_class_entry_ptr, getThis(), ZEND_STRL("ports"), ports TSRMLS_CC);
...
}
添加数组数据
//按特定的字符串 key 添加数据
int add_assoc_null(zval *arg, char *key);
int add_assoc_bool(zval *arg, char *key, int val);
int add_assoc_long(zval *arg, char *key, long val);
int add_assoc_double(zval *arg, char *key, double val);
int add_assoc_resource(zval *arg, char *key, int val);
int add_assoc_string(zval *arg, char *key, char *val, int dup);
int add_assoc_stringl(zval *arg, char *key, char *val, uint len, int dup);
int add_assoc_zval(zval *arg, char *key, zval *val);
//按数组索引下标 index 添加数据
int add_index_null(zval *arg, ulong idx);
int add_index_bool(zval *arg, ulong idx, int val);
int add_index_long(zval *arg, ulong idx, long val);
int add_index_resource(zval *arg, ulong idx, int val);
int add_index_double(zval *arg, ulong idx, double val);
int add_index_string(zval *arg, ulong idx, char *val, int dup);
int add_index_stringl(zval *arg, ulong idx, char *val, uint len, int dup);
int add_index_zval(zval *arg, ulong index, zval *val);
//在数组尾部 append 数据,相当于 $arr[] = $new_append_value;
int add_next_index_null(zval *arg);
int add_next_index_bool(zval *arg, int val);
int add_next_index_long(zval *arg, long val);
int add_next_index_resource(zval *arg, int val);
int add_next_index_double(zval *arg, double val);
int add_next_index_string(zval *arg, char *val, int dup);
int add_next_index_stringl(zval *arg, char *val, uint len, int dup);
int add_next_index_zval(zval *arg, zval *val);
| 参数 | 用途 |
| arg | 将要被添加数据的数组 |
| key / idx | 要添加数据的数组的键值或者索引下标 |
| val | 要添加的特定数据类型的值,这些值将被放到数组HashTable中,注意,这些函数不会使得原始zval的引用计数自动增加 |
| len | add_*_stringl 函数需要用到,用于指定要添加的字符串长度 |
| dup | 对于字符串类型,该标志接受1或0来指明字符串内容是否被拷贝 |
示例:
----php7_wrapper.h----
#if PHP_MAJOR_VERSION < 7 /* PHP Version 5*/
#define sw_add_assoc_string add_assoc_string
#define sw_add_assoc_stringl add_assoc_stringl
#define sw_add_assoc_stringl_ex add_assoc_stringl_ex
#define sw_add_assoc_double_

本文介绍了PHP扩展开发中与数组相关的操作,包括数组初始化、添加数据、获取HashTable、查找元素、创建及销毁HashTable等,并提供了PHP5和PHP7的差异示例。
最低0.47元/天 解锁文章
1413

被折叠的 条评论
为什么被折叠?



