版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xing_____/article/details/79154053
//add_assoc_*系列函数,生成字符索引:
add_assoc_null(zval *aval, char *key);
add_assoc_bool(zval *aval, char *key, zend_bool bval);
add_assoc_long(zval *aval, char *key, long lval);
add_assoc_double(zval *aval, char *key, double dval);
add_assoc_string(zval *aval, char *key, char *strval, int dup);
add_assoc_stringl(zval *aval, char *key,char *strval, uint strlen, int dup);
add_assoc_zval(zval *aval, char *key, zval *value);
//备注:其实这些函数都是宏,都是对add_assoc_*_ex函数的封装。
//add_index_*系列函数,生成整型索引:
ZEND_API int add_index_long (zval *arg, ulong idx, long n);
ZEND_API int add_index_null (zval *arg, ulong idx );
ZEND_API int add_index_bool (zval *arg, ulong idx, int b );
ZEND_API int add_index_resource (zval *arg, ulong idx, int r );
ZEND_API int add_index_double (zval *arg, ulong idx, double d);
ZEND_API int add_index_string (zval *arg, ulong idx, const char *str, int duplicate);
ZEND_API int add_index_stringl (zval *arg, ulong idx, const char *str, uint length, int duplicate);
ZEND_API int add_index_zval (zval *arg, ulong index, zval *value);
//add_next_index_long函数,生成连续的整型索引:
ZEND_API int add_next_index_long (zval *arg, long n );
ZEND_API int add_next_index_null (zval *arg );
ZEND_API int add_next_index_bool (zval *arg, int b );
ZEND_API int add_next_index_resource (zval *arg, int r );
ZEND_API int add_next_index_double (zval *arg, double d);
ZEND_API int add_next_index_string (zval *arg, const char *str, int duplicate);
ZEND_API int add_next_index_stringl (zval *arg, const char *str, uint length, int duplicate);
ZEND_API int add_next_index_zval (zval *arg, zval *value);
ZEND_FUNCTION(sample_array)
{
zval *subarray;
array_init(return_value);
/* Add some scalars */
add_assoc_long(return_value, "life", 42);
add_index_bool(return_value, 123, 1);
add_next_index_double(return_value, 3.1415926535);
/* Toss in a static string, dup'd by PHP */
add_next_index_string(return_value, "Foo", 1);
/* Now a manually dup'd string */
add_next_index_string(return_value, estrdup("Bar"), 0);
/* Create a subarray */
MAKE_STD_ZVAL(subarray);
array_init(subarray);
/* Populate it with some numbers */
add_next_index_long(subarray, 1);
add_next_index_long(subarray, 20);
add_next_index_long(subarray, 300);
/* Place the subarray in the parent */
add_index_zval(return_value, 444, subarray);
}
这时如果我们用户端var_dump这个函数的返回值便会得到:
<?php
var_dump(sample_array());
输出:
array(6)
{
["life"]=> int(42)
[123]=> bool(true)
[124]=> float(3.1415926535)
[125]=> string(3) "Foo"
[126]=> string(3) "Bar"
[444]=> array(3)
{
[0]=> int(1)
[1]=> int(20)
[2]=> int(300)
}
}
void php_xing2233_display(zval **fooval TSRMLS_DC)
{
switch (Z_TYPE_P(*fooval))
{
case IS_NULL:
php_printf("is null");
break;
case IS_BOOL:
if (Z_BVAL_P(*fooval))
{
php_printf("1%s", PHP_EOL);
}
break;
case IS_STRING:
PHPWRITE(Z_STRVAL_P(*fooval), Z_STRLEN_P(*fooval));
break;
case IS_LONG:
php_printf("%ld", Z_LVAL_P(*fooval));
break;
case IS_DOUBLE:
php_printf("%f", Z_DVAL_P(*fooval));
break;
case IS_RESOURCE:
php_printf("resourse #%ld", Z_RESVAL_P(*fooval));
break;
case IS_ARRAY:
php_printf("Array");
break;
case IS_OBJECT:
php_printf("Object");
break;
default:
php_printf("Unknown");
break;
}
}
zval *subarray;
MAKE_STD_ZVAL(subarray);
array_init(subarray);
add_index_long(subarray,111,222);
add_assoc_long(subarray, "hehe", 111);
zend_hash_add(EG(active_symbol_table), "subarray", sizeof("subarray"),
&subarray, sizeof(zval *), NULL);
zval **val;
if (zend_hash_find(EG(active_symbol_table), "subarray", sizeof("subarray"), (void **)&val) == SUCCESS)
{
php_printf("exist%s", PHP_EOL);
} else{
php_printf("not exist", PHP_EOL);
}
//返回当前指针的value
zval **data;
zend_hash_get_current_data(Z_ARRVAL_PP(val), (void**)&data);
php_xing2233_display(data TSRMLS_CC);
//返回当前指针的key
char *key; //当key是string类型时,保存在这个变量
ulong index; //当key是int类型时,保存在这个变量
zend_hash_get_current_key(Z_ARRVAL_PP(val), &key , &index, 0);
php_printf("this is key:%ld%s", index, PHP_EOL);
//根据key得到value
zval **value;
zend_hash_index_find(Z_ARRVAL_PP(val), index, (void**)&value);
php_printf("this is value:");
php_xing2233_display(value TSRMLS_CC);
//向后移动指针
zend_hash_move_forward(Z_ARRVAL_PP(val));
zend_hash_get_current_key(Z_ARRVAL_PP(val), &key , &index, 0);
php_printf("this is key:%s%s", key, PHP_EOL);
//根据key得到value
zend_hash_find(Z_ARRVAL_PP(val), key, strlen(key)+1, (void**)&value);
php_printf("this is value:");
php_xing2233_display(value TSRMLS_CC);
php_printf("%s", PHP_EOL);
//向前移动指针
zend_hash_move_backwards(Z_ARRVAL_PP(val));
zend_hash_get_current_key(Z_ARRVAL_PP(val), &key , &index, 0);
php_printf("this is key:%ld%s", index, PHP_EOL);
return;
//返回数组内的对象总数
RETURN_LONG(Z_ARRVAL_PP(val)->nNumOfElements);
return;
//返回数组
RETURN_ZVAL(subarray, 0, 1);
zend_hash_num_elements 获取数组内元素个数
PHP_FUNCTION(array_chunk)
{
int argc = ZEND_NUM_ARGS(), key_type, num_in;
long size, current = 0;
char *str_key;
uint str_key_len;
ulong num_key;
zend_bool preserve_keys = 0;
zval *input = NULL;
zval *chunk = NULL;
zval **entry;
HashPosition pos;
if (zend_parse_parameters(argc TSRMLS_CC, "al|b", &input, &size, &preserve_keys) == FAILURE) {
return;
}
/* Do bounds checking for size parameter. */
if (size < 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Size parameter expected to be greater than 0");
return;
}
num_in = zend_hash_num_elements(Z_ARRVAL_P(input));
if (size > num_in) {
size = num_in > 0 ? num_in : 1;
}
array_init_size(return_value, ((num_in - 1) / size) + 1);
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void**)&entry, &pos) == SUCCESS) {
/* If new chunk, create and initialize it. */
if (!chunk) {
MAKE_STD_ZVAL(chunk);
array_init_size(chunk, size);
}
/* Add entry to the chunk, preserving keys if necessary. */
zval_add_ref(entry);
if (preserve_keys) {
key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(input), &str_key, &str_key_len, &num_key, 0, &pos);
switch (key_type) {
case HASH_KEY_IS_STRING:
add_assoc_zval_ex(chunk, str_key, str_key_len, *entry);
break;
default:
add_index_zval(chunk, num_key, *entry);
break;
}
} else {
add_next_index_zval(chunk, *entry);
}
/* If reached the chunk size, add it to the result array, and reset the
* pointer. */
if (!(++current % size)) {
add_next_index_zval(return_value, chunk);
chunk = NULL;
}
zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos);
}
/* Add the final chunk if there is one. */
if (chunk) {
add_next_index_zval(return_value, chunk);
}
}