How Can Convert PHP Array Keys to Uppercase or Lowercase ?

22 June 2023 602 Reading time: 57 second

In some cases, array indexes are mixed lowercase and uppercase. In such cases, it may be possible to bring the array indexes to the same format.
You can use the following functions to solve this problem.

To change Array index upperCase

function arrayKeyToUpperCase($array = []){
  $result = [];
  foreach($array as $k => $v) {
      if(is_array($v)) {
          $result[strtoupper($k)] = arrayKeyToUpperCase($v);
      }else{
          $result[strtoupper($k)] = $v;
      }
  }
  return $result;
}

To change Array index lowerCase

function arrayKeysToLowerCase($array = []){
  $result = [];
  foreach($array as $k => $v) {
      if(is_array($v)) {
          $result[strtolower($k)] = arrayKeysToLowerCase($v);
      }else{
          $result[strtolower($k)] = $v;
      }
  }
  return $result;
}

Similar articles