Ruby on Rails
PhpArray_combine

This will use two arrays to create a hash.

Here’s the procedure:


a = ['green', 'red', 'yellow']
b = ["avocado", "apple", "banana"]
c = {}
a.each_with_index do |value, index|
  c[value] = b[index]
end

Here’s it provided as a static Array function.


class Array
  def self.combine(keys, data)
    return false if keys.size != data.size || keys.size == 0

dest = {} keys.each_with_index do |value, index| dest[value] = data[index] end dest end

end

Now you can follow the PHP syntax with a slight difference:


a = ['green', 'red', 'yellow']
b = ["avocado", "apple", "banana"]
c = Array::combine(a,b)

Note: I feel weird about making this an Array:: function. Maybe it should be part of Hash. Or just a free function. Your call.