array_fill_keys
-
This function’s solution uses a Ruby
Hashobject since Ruby arrays don’t use associative key/value pairs. See Array for more details.There is no direct equivalent of the array_fill_keys method in Ruby. We can achieve the same results by building a new hash from existing array information using the
Enumerable#injectmethod.PHP
$keys = array('peel', 'eat'); $result = array_fill_keys($keys, 'orange'); var_export($result); // => array('peel' => 'orange', 'eat' => 'orange')
Ruby
keys = ['peel', 'eat'] result = keys.inject({}) do |hash, key| hash[key] = 'orange' hash end p result # => {"eat"=>"orange", "peel"=>"orange"}
see also
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

