0

For example

a = @(

"test1",

"test2"

)

b = @(

"test3",

"test4"

)

one is

foreach ($c in $a) {

ehco $c

}

What should I do if I have more than one?

5 Answers 5

1

Or create an (ordered) hashtable:

$hashtable = [Ordered]@{ a = @("test1", "test2"); b = @("test3", "test4") }
foreach ($Key in $HashTable.Keys) {
    foreach ($Item in $HashTable[$Key]) {
        echo $Item
    }
}

Yields:

test1
test2
test3
test4
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot do a foreach on more than one object. You can do a for loop, but it will break if the arrays are not of the same size Here's an example with a check on the arrays size:

if ($a.Count -eq $b.Count)
{
    for ($i=0; $i -lt $a.count; $i++)
    {
        $a[$i]
        $b[$i]
    }
}

1 Comment

What if b is greater than a?
0

You could do it like this:

$a = @(
"test1",
"test2"

)
$b = @(
"test3",
"test4"
)
foreach ($c in ($a,$b)) {
    echo $c
}

Comments

0

You can add the arrays and then loop through each item.

$a = @(

"test1",

"test2"

)

$b = @(

"test3",

"test4"

)

foreach($item in ($a + $b))
{
write-host $item
} 
test1
test2
test3
test4

Comments

0
$a = @("test1","test2"); $b = @("test3","test4")
$a,$b | % {$_}

test1
test2
test3
test4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.