array_change_key_case

This function changes all keys in an array by returning an array with all keys from argument lowercased or uppercased. Numbered indices are left as is.

PHP

$input_array = array('FirSt' => 1, 'SecOnd' => 4);
print_r( array_change_key_case($input_array, CASE_UPPER) );
// => array('FIRST' => 1, 'SECOND' => 4);

To replicate this functionality in Ruby, we need to use a Hash object, since arrays in Ruby don’t use associative key/value pairs.

Ruby

hash = {'FirSt' => 1, 'SecOnd' => 4}
 
result = hash.inject({}) do |hash, keys|
  hash[keys[0].upcase] = keys[1]
  hash
end
p result
# => { 'FIRST' => 1, 'SECOND' => 4 }
By | 2009-10-15T22:42:53+00:00 March 28th, 2009|Categories: PHP, Ruby|Tags: , , , , |1 Comment

About the Author:

One Comment

  1. Boris Barroso August 2, 2011 at 6:55 pm

    hash = {‘FirSt’ => 1, ‘SecOnd’ => 4}
    p Hash[hash.keys.map(&:upcase).zip(hash.values)]
    # => {“FIRST”=>1, “SECOND”=>4}

Comments are closed.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.

This Is A Custom Widget

This Sliding Bar can be switched on or off in theme options, and can take any widget you throw at it or even fill it with your custom HTML Code. Its perfect for grabbing the attention of your viewers. Choose between 1, 2, 3 or 4 columns, set the background color, widget divider color, activate transparency, a top border or fully disable it on desktop and mobile.