Skip to content

Instantly share code, notes, and snippets.

@chadsaun
Last active December 25, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chadsaun/7043210 to your computer and use it in GitHub Desktop.
Save chadsaun/7043210 to your computer and use it in GitHub Desktop.
A function to rename keys from an array you provide.
<?php defined('SYSPATH') OR die('No direct script access.');
class Arr extends Kohana_Arr {
/**
* Maps new keys to an array
*
* Example:
*
* $customer_values = Arr::remap_keys($_POST, array(
* 'billing_address1' => 'address1', 'billing_address2' => 'address2'
* ));
*
* @param $values
* @param $new_keys
*
* @return array
*/
public static function remap_keys($values, $new_keys) {
$new_array = array();
foreach ($values as $key => $value) {
// if there is a new key to assign
if (array_key_exists($key, $new_keys)) {
$new_array[$new_keys[$key]] = $value;
} else {
$new_array[$key] = $value;
}
}
return $new_array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment