# Combining two arrays

**URL:** <https://www.ruby-forum.com/t/combining-two-arrays/69684>\
**Category:** Rails\
**Created:** [September 22, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/combining-two-arrays/69684 "2006-09-22T23:56:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [September 22, 2006, 11:56pm UTC](https://www.ruby-forum.com/t/combining-two-arrays/69684/1 "2006-09-22T23:56:33Z")

</div>

Hi,

I have two arrays: one that contains ids and data values and one that  
contains ids and names. There is a relationship between the ids in the  
two arrays. How do I create a third array that contains names and data  
values? This is basically a lookup to replace ids pulled from a  
database with a readable name.

Thanks,

David

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [September 23, 2006, 1:46am UTC](https://www.ruby-forum.com/t/combining-two-arrays/69684/2 "2006-09-23T01:46:43Z")

</div>

I assume you mean’t Hashes and not Arrays?

This assumes the elements are in the same order:

names = {:one =\> “one”, :two =\> “two”}  
data = {“one” =\> “foo”, “two” =\> “bar”}  
hash = {}  
names.values.zip(data.values).each {|key, value| hash[key] = value}  
hash  
=\> {“two”=\>“bar”, “one”=\>“foo”}

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [September 23, 2006, 3:21am UTC](https://www.ruby-forum.com/t/combining-two-arrays/69684/3 "2006-09-23T03:21:42Z")

</div>

I’m using find to return to sets of data from a database that need to be  
combined to display in an html table. The id is in one array, but the  
corresponding name I need to display is in the other array because it’s  
in another table. How do I combine them so I can display the name in  
the table instead of the id?

Or is it better to do a join using rails so I only return a single set  
of data from the database?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [September 24, 2006, 3:20am UTC](https://www.ruby-forum.com/t/combining-two-arrays/69684/4 "2006-09-24T03:20:33Z")

</div>

I’d go with a JOIN, it’s no doubt faster.

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [September 23, 2006, 4:10am UTC](https://www.ruby-forum.com/t/combining-two-arrays/69684/5 "2006-09-23T04:10:51Z")

</div>

Array#zip

Or, a join.

On 9/22/06, David L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks,
> 
> David
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
When a man says he approves of something in principle, it means he  
hasn’t the slightest intention of putting it into practice. –  
Bismarck
