array_combine
-
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
array_combinein Ruby. We can get similar results by manually building a hash from two arrays.PHP
$make = array('VW', 'Ford'); $model = array('Jetta', 'Explorer'); $result = array_combine($make, $model); var_export($result); // => array('VW' => 'Jetta', 'Ford' => 'Explorer')
Ruby
make = ['VW', 'Ford'] model = ['Jetta', 'Explorer'] result = [] model.each_with_index do |val, key| result[make[key]] = val end p result # => {"VW"=>"Jetta", "Ford"=>"Explorer"}
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.

