我如何从php扩展返回一个数组?

时间:2016-03-17 17:40:03

标签: php c php-internals

如何从php扩展方法返回数组?这就是我到目前为止所做的:

struct myClass_object
{
    zend_object std;
    MyClass *myClassInstance;
};

PHP_METHOD(MyClass, getMyArray)
{
    zval *obj = getThis();
    MyClass *myClass = (myClass_object *) zend_object_store_get_object (obj TRMLS_CC)->myClassInstance;
    if (myClass != NULL)
    {
        int length = myClass->getMyArrayLength();
        int *myArray = myClass->getMyArray();
        // return myArray!
    }
    RETURN_NULL();
}

1 个答案:

答案 0 :(得分:1)

在检查了一些php源文件之后,我能够返回一个这样的数组:

PHP_METHOD(MyClass, getMyArray)
{
    zval *obj = getThis();
    MyClass *myClass = (myClass_object *) zend_object_store_get_object (obj TRMLS_CC)->myClassInstance;
    if (myClass != NULL)
    {
        int length = myClass->getMyArrayLength();
        int *myArray = myClass->getMyArray();
        array_init(return_value);
        for (int i = 0; i < length; i++) add_index_long(return_value, i, myArray[i]);
        return;
    }
    RETURN_NULL();
}