从 php 扩展中读取数组(简单读取)
Reading an array from a php extension (simple read )
我现在正在为 BladeRF 卡创建一个 php 扩展,我有一个数组可以从这样的扩展中读取:
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) {
RETURN_NULL();
}
但我的问题是如何在我的扩展程序中循环这个数组?
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer)== SUCCESS;zend_hash_move_forward_ex(arr_hash, &pointer)) {
//I know that i have to loop using this loop but my problem is how to recuperate all array field value in this loop
}
manual 描述了它。示例:
for(
zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
zend_hash_move_forward_ex(arr_hash, &pointer)
) {
if (Z_TYPE_PP(data) == IS_STRING) {
PHPWRITE(Z_STRVAL_PP(data), Z_STRLEN_PP(data));
php_printf("\n");
}
/* and so on ... */
}
我不确定这是否完全回答了您的问题。不要犹豫,索取更多信息。
这是一个完整的例子:
hello.c
...
PHP_FUNCTION(hello_world)
{
HashTable *names_hash;
HashPosition pointer;
zval *names, **data;
int array_count;
if(zend_parse_parameters(
ZEND_NUM_ARGS() TSRMLS_CC,
"a", &names
) != SUCCESS) {
RETURN_FALSE;
}
names_hash = Z_ARRVAL_P(names);
array_count = zend_hash_num_elements(names_hash);
for(
zend_hash_internal_pointer_reset_ex(names_hash, &pointer);
zend_hash_get_current_data_ex(names_hash, (void**) &data, &pointer) == SUCCESS;
zend_hash_move_forward_ex(names_hash, &pointer)
) {
if (Z_TYPE_PP(data) == IS_STRING) {
do_say_hello(Z_STRVAL_PP(data));
}
}
RETURN_TRUE;
}
void do_say_hello(char *name) {
printf("Hello %s!\n", name);
}
...
hello.php
<?php
hello_world(array('foo', 'bar'));
我现在正在为 BladeRF 卡创建一个 php 扩展,我有一个数组可以从这样的扩展中读取:
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) {
RETURN_NULL();
}
但我的问题是如何在我的扩展程序中循环这个数组?
for(zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer)== SUCCESS;zend_hash_move_forward_ex(arr_hash, &pointer)) {
//I know that i have to loop using this loop but my problem is how to recuperate all array field value in this loop
}
manual 描述了它。示例:
for(
zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
zend_hash_move_forward_ex(arr_hash, &pointer)
) {
if (Z_TYPE_PP(data) == IS_STRING) {
PHPWRITE(Z_STRVAL_PP(data), Z_STRLEN_PP(data));
php_printf("\n");
}
/* and so on ... */
}
我不确定这是否完全回答了您的问题。不要犹豫,索取更多信息。
这是一个完整的例子:
hello.c
...
PHP_FUNCTION(hello_world)
{
HashTable *names_hash;
HashPosition pointer;
zval *names, **data;
int array_count;
if(zend_parse_parameters(
ZEND_NUM_ARGS() TSRMLS_CC,
"a", &names
) != SUCCESS) {
RETURN_FALSE;
}
names_hash = Z_ARRVAL_P(names);
array_count = zend_hash_num_elements(names_hash);
for(
zend_hash_internal_pointer_reset_ex(names_hash, &pointer);
zend_hash_get_current_data_ex(names_hash, (void**) &data, &pointer) == SUCCESS;
zend_hash_move_forward_ex(names_hash, &pointer)
) {
if (Z_TYPE_PP(data) == IS_STRING) {
do_say_hello(Z_STRVAL_PP(data));
}
}
RETURN_TRUE;
}
void do_say_hello(char *name) {
printf("Hello %s!\n", name);
}
...
hello.php
<?php
hello_world(array('foo', 'bar'));