public
Last active

A function to rename keys from an array you provide.

  • Download Gist
Arr.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<?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;
 
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.