0

I'm working on a project,

I have 3 arrays, and i want to take all values from it. maybe some code would be better than explanation.

$Array1= "Value1", "Value2", ....
$Array2= "Another1", "Another2",...
$Array3= "Again1", "Again2", ...

My result should be

Value1 Another1 Again1
Value2 Another2 Again2

etc...

I've tried many option like so :

foreach($i in $Array1){
  Foreach($j in $Value2){
     Write-host $i "," $j
   }
} 

result:
Value1, Another1
Value1,Another2
Value2,Another1
Value2,Another2

I've search a solution for like 5 hours . and i'm not an expert in powershell. please help me !!

1 Answer 1

1

You need to make sure either all arrays have the same number of elements, or take the minimum amount of elements to loop through:

$Array1= "Value1", "Value2" #
$Array2= "Another1", "Another2"#
$Array3= "Again1", "Again2"#

# make sure you do not step out of index on one of the arrays
$items = [math]::Min($Array1.Count, [math]::Min($Array2.Count, $Array3.Count))

for ($i = 0; $i -lt $items; $i++) {
    # using the -f Format operator gives nice-looking code I think
    '{0},{1},{2}' -f $Array1[$i], $Array2[$i], $Array3[$i]
}

Result:

Value1,Another1,Again1
Value2,Another2,Again2
Sign up to request clarification or add additional context in comments.

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.