How to get 2 dimensional array with foreach in PowerShell?

Cheries

I have 2 dimension array. I would like to get each value of array. I tried this, but each array in $Name has all the array in $Date. But my expectation first item in $Name equal to first item in $Date. This is my expectation result:

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
     }
}

but from the code above I got this result

A\11
A\12
A\13
B\11
B\12
B\13
C\11
C\12
C\13

Anyone can help, please. Thank you

Theo

When combining two arrays, both having a non-fixed number of elements, you need to make sure you do not index beyond any of the arrays indices.

The easiest thing here is to use an indexed loop, where you set the maximum number of iterations to the minimum number of array items:

$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]
}

Output:

A\11
B\12
C\13

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a multi dimensional array in a foreach loop

How to get input and display 2 dimensional array in c++?

How to get 2dimensional array from file (C++)

How to improve this with a 2 dimensional array

AJAX: How to get array on multi dimensional array

How to group array 1 dimensional to array 2 dimensional?

How to flatten a 2-dimensional array into a 1-dimensional array

how to copy a 3 dimensional array to a 2 dimensional array?

How to convert multidimensional array into a 2 dimensional array?

How to get the "edges" of a single dimensional array

How to get sum of two dimensional array

How to loop multi dimensional array to get values

how to foreach 2 array

How to force numpy to interpret 2 dimensional array as 1 dimensional

How do I get the largest and smallest 2 in a 2 dimensional array in dart?

How to select the property of an object in a two dimensional array with forEach()?

How to retrieve string from a 2 dimensional array

How to concatenate 2 strings in a multi dimensional array?

How to build a histogram of numpy 2 dimensional array

How to create two dimensional array with 2 arrays

How to convert 2 dimensional array to Set?

How to instantiate a class object with a 2 dimensional array?

PHP how to aggregate 2 dimensional array

How to find 2 dimensional array size in JNI

how to use 2 Dimensional array in Verilog

Javascript: How to initialize a 2 dimensional array

How to add to a value to a specific 2 dimensional array

How to sort 2 dimensional array (matrix) in JavaScript?

How to switch two columns in a 2 dimensional array?