Powershell 添加到多维数组 [英] Powershell add to Multidimensional Array

查看:67
本文介绍了Powershell 添加到多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样在 powershell 中创建一个多维数组:

I want to create a multidimensional array in powershell like this:

$array[0] = "colours"
$array[0][0] = "red"
$array[0][1] = "blue"
$array[1] = "animals"
$array[1][0] = "cat"
$array[1][0] = "dog"

这是我尝试过的:

$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"

我感谢任何提示.

提前致谢

推荐答案

看起来使用 [hashtable](也称为关联数组)会更好:

It looks like you'd be better off with a [hashtable] (also called an associative array):

$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'

这篇关于Powershell 添加到多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆