Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $new = array();
- foreach( $old as $key=>$value) {
- $key = strToLower($key);
- if(!array_key_exists($key,$new) {
- $new[$key] = $value;
- }
- else {
- throw new Exception('Duplicate Key Encountered');
- }
- }
- $lowerCaseKeys = array_map('strtolower', array_keys($array));
- $duplicates = array_filter(array_count_values($lowerCaseKeys), create_function('$count', 'return $count > 1;'));
- if (!empty($duplicates)) {
- throw new Exception('duplicate keys found: ' . implode(',', array_keys($duplicates)));
- }
- # Recreate the array with lower-case keys
- $array = array_combine($lowerCaseKeys, array_values($array));
- $lowerCaseKeyArray = array_change_key_case($array);
- if (count($lowerCaseKeyArray) !== count($array)) {
- # You can extract the duplicate keys here as above, if you like
- throw new Exception('duplicate keys found!');
- }
- $new = array_change_key_case($old, CASE_LOWER);
- if (count($new) < count($old)) {
- throw new Exception("Duplicate key encountered.");
- }
- foreach(array_keys($old) as $key) {
- $lower = strtolower($key);
- //if key is already lower case, do nothing
- if($key == $lower)
- continue;
- $value = $old[$key];
- unset($old[$key]);
- $old[$lower] = $value;
- }
Add Comment
Please, Sign In to add comment