Guest User

Untitled

a guest
May 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. $new = array();
  2. foreach( $old as $key=>$value) {
  3. $key = strToLower($key);
  4. if(!array_key_exists($key,$new) {
  5. $new[$key] = $value;
  6. }
  7. else {
  8. throw new Exception('Duplicate Key Encountered');
  9. }
  10.  
  11. }
  12.  
  13. $lowerCaseKeys = array_map('strtolower', array_keys($array));
  14. $duplicates = array_filter(array_count_values($lowerCaseKeys), create_function('$count', 'return $count > 1;'));
  15. if (!empty($duplicates)) {
  16. throw new Exception('duplicate keys found: ' . implode(',', array_keys($duplicates)));
  17. }
  18. # Recreate the array with lower-case keys
  19. $array = array_combine($lowerCaseKeys, array_values($array));
  20.  
  21. $lowerCaseKeyArray = array_change_key_case($array);
  22. if (count($lowerCaseKeyArray) !== count($array)) {
  23. # You can extract the duplicate keys here as above, if you like
  24. throw new Exception('duplicate keys found!');
  25. }
  26.  
  27. $new = array_change_key_case($old, CASE_LOWER);
  28. if (count($new) < count($old)) {
  29. throw new Exception("Duplicate key encountered.");
  30. }
  31.  
  32. foreach(array_keys($old) as $key) {
  33. $lower = strtolower($key);
  34. //if key is already lower case, do nothing
  35. if($key == $lower)
  36. continue;
  37. $value = $old[$key];
  38. unset($old[$key]);
  39. $old[$lower] = $value;
  40. }
Add Comment
Please, Sign In to add comment