1

假设我有两个这样的数组:

$greetings = "hello","hola","namaste"

$names = "jon","jim"

如果我需要这样的输出:

hello jon
hola jon
namaste jon
hello jim
hola jim
namaste jim

我该怎么办?

我试过这个:

$greetings | % { $_ + " " + $names}

这只是给我这样的输出:

hello jon jim
hola jon jim
namaste jon jim
4

2 回答 2

3

一条线:

$names | % { foreach($greeting in $greetings) { "$greeting $_" }}
于 2013-02-28T13:44:28.000 回答
2

你需要一个嵌套循环:

$greetings = "hello","hola","namaste"

$names = "jon","jim"

Foreach ($name in $names)
 {
  Foreach ($greeting in $greetings)
   {
    "$greeting $name"
   }
 }
于 2013-02-28T13:34:36.213 回答