How do you read from a multidimensional version of an array returned from a COM object in PHP?

I am working with a COM object that returns a multi-dimensional VARIANT array (vt_array), and I'm trying to read values ​​from an array.

When I use print_r($mdArray), it displays variant Object. ( variant_get_type($mdArray)returns 8204.)

I tried to use foreach ($mdArray as $oneArray), but I got a message:

Warning: Loader :: getfields () [loader.getfields]: can only process one-dimensional array options (this array has 2) in C: \ Inetpub \ Wwwroot \ root \ script \ fileloader.php on line 135 Fatal error: unclean exception " The exception is' with a message 'An object of type variant did not create an Iterator' in C: \ Inetpub \ Wwwroot \ root \ script \ fileloader.php: 135 Stack trace: # 0 C: \ Inetpub \ Wwwroot \ root \ script \ fileloader.php (135 ): Loader :: getfields () # 1 C: \ Inetpub \ Wwwroot \ root \ testloader.php (21): Loader-> getfields () # 2 {main} throw in C: \ Inetpub \ Wwwroot \ root \ script \ fileloader.php on line 135

(foreach loop is on line 135)

The only information I can get about the array is to use count($mdArray)which returns 8.

If anyone has experience reading from VARIANT multidimensional arrays, please tell me how to do this.

+3
source share
1 answer

Try extracting the array values ​​through "VBScript". Yes, you read it 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";
    }

?>

It is well tested in the array created by VBScript, which otherwise gave me the same problems and errors as you, trying to make it behave like a PHP array. The above method, spawned by the unholy combination of PHP and VBscript, should retrieve values ​​piecemeal just fine.

To explain $y1 = 0; $y2 = 1;, keep in mind that the parameters of the VBScript function are byref, so you cannot pass anything but a variable.

: $com->AllowUI = false, . , MsgBox() VBScript, , "ok".

+4

Source: https://habr.com/ru/post/1787852/


All Articles