I am writing a php extension for my C ++ library, which is defined something like this:
bool getPids(map<string,string> pidsMap, vector<string> ids);
Now, I am writing a php wrapper for the above function like this.
ZEND_METHOD(myFInfo, get_pids) { zval *idsArray; if (zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "a", &idsArray ) == FAILURE ) { RETURN_NULL(); } }
now I want to call getPids (), but I donβt know how to pass idsArray as a vector to the C ++ function correctly.
after some searching on the internet, I found an example where the zval array is iterated to read all the values, and I thought that maybe I could use this to create a vector.
PHP_FUNCTION(hello_array_strings) { zval *arr, **data; HashTable *arr_hash; HashPosition pointer; int array_count; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) { RETURN_NULL(); } arr_hash = Z_ARRVAL_P(arr); array_count = zend_hash_num_elements(arr_hash); php_printf("The array passed contains %d elements", array_count); 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(" "); } } RETURN_TRUE; }
but is this a better approach? or is there a better way to do this?
thanks!
source share