dongtan1009 2017-01-28 22:05
浏览 29

传递一个php7数组值作为参考

I found a note on this site which was very helpful in converting a PHP5 extension to PHP7. I seem to be stuck on another issue.

I have a function in a C extension that looks like this

PHP_FUNCTION(GetSystemInformation)
{
struct dsiDeviceIdParams deviceParams[WS_MAX_STACKABLE_UNITS];
zval   avar[WS_MAX_STACKABLE_UNITS];
int    num, ret, i;

zval   *adeviceParams;
zval   *znumberOfElementsInList;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/z/",
    &adeviceParams, &znumberOfElementsInList) == FAILURE)
{
    return;
} 

ret = dsiGetSystemInformation(deviceParams, &num);
if (WS_OK != ret)
{
      RETURN_LONG(ret);

// i added these and the "a/z/" flags
zval_dtor(znumberOfElementsInList);
zval_dtor(adeviceParams); // i tried moving this to the end of the function too

znumberOfElementsInList->value.lval = num; // this value works! i can see it back on the webpage PHP 


array_init(adeviceParams);

if (num > WS_MAX_STACKABLE_UNITS)
 {
    num = WS_MAX_STACKABLE_UNITS;
  }

 if (num > 0 && num <= WS_MAX_STACKABLE_UNITS)
 {
    for (i = 0; i < num; ++i)
    {
        //MAKE_STD_ZVAL(avar[i]);
        array_init(&avar[i]);

        sprintf(txt,"d1 %s
",deviceParams[i].boxNumber); dump(txt);

        add_assoc_long_ex(&avar[i], "boxNumber", sizeof("boxNumber"),
                deviceParams[i].boxNumber);
        sprintf(txt,"d2 %s
",deviceParams[i].modelNumber); dump(txt);

        local_add_assoc_string_ex(&avar[i], "modelNumber", sizeof("modelNumber"),
                deviceParams[i].modelNumber, 1);

    sprintf(txt,"d3 %s
",deviceParams[i].versionNumber); dump(txt);
    local_add_assoc_string_ex(&avar[i], "versionNumber", sizeof("versionNumber"),
            deviceParams[i].versionNumber, 1);

    sprintf(txt,"d4 %s
",deviceParams[i].serialNumber); dump(txt);
    local_add_assoc_string_ex(&avar[i], "serialNumber", sizeof("serialNumber"),
            deviceParams[i].serialNumber, 1);

    sprintf(txt,"d5 %d
",deviceParams[i].numberOfPorts); dump(txt);
    add_assoc_long_ex(&avar[i], "numberOfPorts", sizeof("numberOfPorts"),
            deviceParams[i].numberOfPorts);

    dump("D6
");
    add_next_index_zval(adeviceParams, &avar[i]);
    dump("DE
");
 }
}


RETURN_LONG(ret);
 }

it is called in PHP like this

I get the value back properly for NumberOfElements. However, The deviceParams value is null/empty and it doesn't seem to be able to find any of the array fields

A PHP Error was encountered Severity: Notice Message: Undefined index: modelNumber Filename: controllers/login.php Line Number: 158

If i run this line

debug_zval_dump( $deviceParams );

I can see the content (i.e model number/etc).

array(1) refcount(2){ [0]=> array(5) refcount(1){ ["boxNumber"]=> int(0) ["modelNumber"]=> string(9) "VS-1224-F" refcount(1) ["versionNumber"]=> string(12) "1.4.20160607" refcount(1) ["serialNumber"]=> string(8) "15090500" refcount(1) ["numberOfPorts"]=> int(24) } }

I am sure this has something to do with reference pointers/etc and am not yet sure how to resolve this. Can someone point me in the right direction?

Basically, I want to pass an array into my C extension, change the contents of the array (mostly populating it), and then see the contents back at the PHP level for presentation in the webpage. I can get all the way through except the presentation part.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 如何绘制动力学系统的相图