Combine Individual Items From Separate Arrays Into a Third Array

I have two arrays, one with a list of first names and a second with a list of last names. What I would like to do is combine the first name and the last name that reside at the same index position in their respective arrays into a third array. Each pair of arrays will always have the same number of items, but that number may change - i.e., in some instances there may be 5 first names and 5 last names, but at other times there may be 15 first names and 15 last names.

So, for example:

Array $firstName contains:
$firstName[0] = Jon
$firstName[1] = Bob
$firstName[2] = Tom

Array $lastName contains:
$lastName[0] = Smith
$lastName[1] = Jones
$lastName[2] = White

And I want to create array $fullName like this...
$fullName[0] = Jon Smith
$fullName[1] = Bob Jones
$fullName[2] = Tom White

Thanks for the help.

September 11th, 2015 11:11am

If you know for certain that the first and last names will always be in the correct order, you can do this.

$firstname = @('Jon','Bob','Tom')
$lastname = @('Smith','Jones','White')
$fullname = @()
for ($i = 0; $i -lt $firstname.count; $i++)
{
    $fullname += "$($firstname[$i]) $($lastname[$i])"
}
$fullname

More reading on For loops: http://ss64.com/ps/for.html

  • Proposed as answer by Mike Laughlin 15 hours 32 minutes ago
  • Marked as answer by awf1130 15 hours 11 minutes ago
Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 11:32am

Works perfectly. Thanks for the help!
September 11th, 2015 12:02pm

Different variation using ArrayList instead of appending to the array.

$fn = 'Jon','Bob','Tom','Bill'
$ln = 'Smith','Jones','White','Jackson'
$name =  New-Object -TypeName System.Collections.ArrayList
$cnt = $fn.Count
if ($ln.Count -ne $cnt) { "Arrays out of sync"; exit }
for ($i = 0; $i -lt $cnt; $i++)
{
    $name.Add("$($fn[$i]) $($ln[$i])") | Out-Null
}
$name

Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 1:04pm

If you know for certain that the first and last names will always be in the correct order, you can do this.

$firstname = @('Jon','Bob','Tom')
$lastname = @('Smith','Jones','White')
$fullname = @()
for ($i = 0; $i -lt $firstname.count; $i++)
{
    $fullname += "$($firstname[$i]) $($lastname[$i])"
}
$fullname

More reading on For loops: http://ss64.com/ps/for.html

  • Proposed as answer by Mike Laughlin Friday, September 11, 2015 3:39 PM
  • Marked as answer by awf1130 Friday, September 11, 2015 4:00 PM
September 11th, 2015 3:30pm

Different variation using ArrayList instead of appending to the array.

$fn = 'Jon','Bob','Tom','Bill'
$ln = 'Smith','Jones','White','Jackson'
$name =  New-Object -TypeName System.Collections.ArrayList
$cnt = $fn.Count
if ($ln.Count -ne $cnt) { "Arrays out of sync"; exit }
for ($i = 0; $i -lt $cnt; $i++)
{
    $name.Add("$($fn[$i]) $($ln[$i])") | Out-Null
}
$name

  • Proposed as answer by Mike Laughlin Friday, September 11, 2015 5:14 PM
Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 5:02pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics