If the display order between the key and the pairs should be based on the first element in array2, you do not need to array:
array2 = [
["apple", "good taste", "red"],
["lemon" , "no taste", "yellow"],
["orange", "bad taste", "orange"]
]
map = Hash[ array2.map{ |a| [a.first,a] } ]
p map
#=> {
#=> "apple"=>["apple", "good taste", "red"],
#=> "lemon"=>["lemon", "no taste", "yellow"],
#=> "orange"=>["orange", "bad taste", "orange"]
#=> }
If you want to use a arraysubset of elements to select, you can do this:
# Use the map created above to find values efficiently
array = %w[orange lemon]
hash = Hash[ array.map{ |val| [val,map[val]] if map.key?(val) }.compact ]
p hash
#=> {
#=> "orange"=>["orange", "bad taste", "orange"],
#=> "lemon"=>["lemon", "no taste", "yellow"]
#=> }
The code if map.key?(val)also compactensures that there is no problem if it arrayrequests keys that are absent in array2, and does so in O(n)time.