Return Multidimensional Array From Function

前端 未结 2 1075
后悔当初
后悔当初 2020-12-02 00:56

I encountered some issue in converting my existing vbs script to PowerShell script. I have illustrate here with some dummy codes instead of my original code. In example 1,

2条回答
  •  眼角桃花
    2020-12-02 01:23

    PowerShell unrolls arrays returned from a function. Prepend the returned array with the comma operator (,, unary array construction operator) to wrap it in another array, which is unrolled on return, leaving the nested array intact.

    function testArray {
        $array1 = @()
        $array1 += ,@("Apple","Banana")
    
        return ,$array1
    }
    

提交回复
热议问题