fus*_*596 2 powershell multidimensional-array
我想在 powershell 中创建一个多维数组,如下所示:
$array[0] = "colours"
$array[0][0] = "red"
$array[0][1] = "blue"
$array[1] = "animals"
$array[1][0] = "cat"
$array[1][0] = "dog"
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
$array = @()
$array += "colours"
$array += "animals"
$array[0] # outputs "colours"
$array[1] # outputs "animals"
$array[0] = @()
$array[1] = @()
$array[0] += "red"
$array[0] += "blue"
$array[1] += "cat"
$array[1] += "dog"
$array[0] # outputs "red", "blue" - i expected "colours" here
$array[0][0] # outputs "red"
Run Code Online (Sandbox Code Playgroud)
我很欣赏任何提示。
提前致谢
看起来你最好使用[hashtable]
(也称为关联数组):
$hash = @{
colours = @('red','blue')
animals = @('cat','dog')
}
$hash.Keys # show all the keys
$hash['colours'] # show all the colours
$hash.colours # same thing
$hash['colours'][0] # red
$hash['foods'] = @('cheese','biscuits') # new one
$hash.clothes = @('pants','shirts') #another way
$hash.clothes += 'socks'
Run Code Online (Sandbox Code Playgroud)