Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在PowerShell中使用foreach获取二维数组?_Arrays_Powershell_Multidimensional Array - Fatal编程技术网

Arrays 如何在PowerShell中使用foreach获取二维数组?

Arrays 如何在PowerShell中使用foreach获取二维数组?,arrays,powershell,multidimensional-array,Arrays,Powershell,Multidimensional Array,我有二维数组。我想得到数组的每个值。 我试过了,但是$Name中的每个数组都有$Date中的所有数组。但我期望$Name中的第一项等于$Date中的第一项。 这是我的期望结果: A\11 B\12 C\13 $Name = @("A", "B", "C") #the size of this array not fixed $Date = @("11", "12", "13"

我有二维数组。我想得到数组的每个值。 我试过了,但是$Name中的每个数组都有$Date中的所有数组。但我期望$Name中的第一项等于$Date中的第一项。 这是我的期望结果:

A\11
B\12
C\13

$Name = @("A", "B", "C") #the size of this array not fixed
$Date = @("11", "12", "13") #the size of this array not fixed

foreach ($na in $Name)
{
     foreach ($dt in $Date)
     {
          $NameAndDate = "$na\$dt"
          Write-Host $NameAndDate
     }
}
但是从上面的代码中我得到了这个结果

A\11
A\12
A\13
B\11
B\12
B\13
C\11
C\12
C\13
任何人都可以帮忙。谢谢

试试这个

$Name = @("A", "B", "C") #the size of this array not fixed
$Date = @("11", "12", "13") #the size of this array not fixed
foreach ($i in 0..($Name.Count -1)) {
    write-host ($Name[$i] + "\" + $Date[$i])
}
试试这个

$Name = @("A", "B", "C") #the size of this array not fixed
$Date = @("11", "12", "13") #the size of this array not fixed
foreach ($i in 0..($Name.Count -1)) {
    write-host ($Name[$i] + "\" + $Date[$i])
}

当组合两个数组时,两个数组都有非固定数量的元素,您需要确保索引不超过任何数组索引

这里最简单的方法是使用索引循环,将最大迭代次数设置为最小数组项数:

$Name = "A", "B", "C"    # the size of this array not fixed
$Date = "11", "12", "13" # the size of this array not fixed

# make sure you do not index beyond any of the array indices
$maxLoop = [math]::Min($Name.Count, $Date.Count)

for ($i = 0; $i -lt $maxLoop; $i++) {
    # output the combined values
    '{0}\{1}' -f $Name[$i], $Date[$i]
}
输出:

A\11
B\12
C\13

当组合两个数组时,两个数组都有非固定数量的元素,您需要确保索引不超过任何数组索引

这里最简单的方法是使用索引循环,将最大迭代次数设置为最小数组项数:

$Name = "A", "B", "C"    # the size of this array not fixed
$Date = "11", "12", "13" # the size of this array not fixed

# make sure you do not index beyond any of the array indices
$maxLoop = [math]::Min($Name.Count, $Date.Count)

for ($i = 0; $i -lt $maxLoop; $i++) {
    # output the combined values
    '{0}\{1}' -f $Name[$i], $Date[$i]
}
输出:

A\11
B\12
C\13

这回答了你的问题吗@iRon不,这不是我的问题的答案。你是想把两个一维数组合并成一个二维数组吗?@SBR-你的问题没有完全的意义,你的预期输出中应该有“B\13”吗如果您没有完全声明什么逻辑应该决定这一点,如果不是这样,我建议使用for循环:
for($I=0;$I-lt$name.count;$I++){($name[$I])+“\”+($Date[$I])}
请解释您期望的输出@jfmilner answer返回
A\11 B\12 C\13
我猜您实际期望的结果(以及副本中描述的内容)。但在您的示例脚本中,顶部有
A\11b\12b\13c\13
,这就是您期望的输出吗?如果是,请解释
B\13
的来源。这是否回答了您的问题@iRon不,这不是我的问题的答案。你是想把两个一维数组合并成一个二维数组吗?@SBR-你的问题没有完全的意义,你的预期输出中应该有“B\13”吗如果您没有完全声明什么逻辑应该决定这一点,如果不是这样,我建议使用for循环:
for($I=0;$I-lt$name.count;$I++){($name[$I])+“\”+($Date[$I])}
请解释您期望的输出@jfmilner answer返回
A\11 B\12 C\13
我猜您实际期望的结果(以及副本中描述的内容)。但在您的示例脚本中,顶部有
A\11b\12b\13c\13
,这就是您期望的输出吗?如果是,请解释
B\13
的来源。