How do you read from a multidimensional variant array returned from a COM object in PHP?

人盡茶涼 提交于 2019-12-01 09:27:02

Try this to extract array values through "VBScript". Yes, you read that right...

<?php

$com = new COM("MSScriptControl.ScriptControl");
$com->Language = 'VBScript';
$com->AllowUI = false;
$com->AddCode('
    Function getArrayVal(arr, indexX, indexY)
        getArrayVal = arr(indexX, indexY)
    End Function
');

$y1 = 0;
$y2 = 1;
for ($x=0; $x < count($mdArray); $x++) {
    echo $com->Run('getArrayVal', $mdArray, $x, $y1) . ": ";
    echo $com->Run('getArrayVal', $mdArray, $x, $y2) . "\n";
    }

?>

Tested good on a VBScript-created array, which otherwise gave me the exact same issues and errors as you when trying to coerce it to behave like a PHP array. The above method spawned by the unholy union of PHP and VBscript should extract values piece by piece just fine.

To explain $y1 = 0; $y2 = 1;, keep in mind the parameters of the VBScript function are byref, so you can't pass anything in except a variable.

Edit: Added $com->AllowUI = false to shut off any on-screen popups. Otherwise it would freeze the request if a MsgBox() somehow got called from VBScript and no one was at the server terminal to click 'ok'.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!