Learn How to Convert PHP Array Keys to Uppercase or Lowercase by using the PHP array_change_key_case() Function.
PHP array_change_key_case() Function
The PHP array_change_key_case() Function is used to convert all the keys to the Uppercase or Lowercase of an Array.
This function returns an array where all the array keys are uppercase or lowercase according to your case option.
Syntax
array_change_key_case(array, case);
- array – This parameter takes an array.
- case – This parameter takes the case option –
CASE_UPPER
– Changes the keys to UppercaseCASE_LOWER
– Changes the keys to Lowercase
Uppercase Example
<?php
$my_array = array("name" => "John Doe", "Age" => 21, "emAil" => "[email protected]");
// New array with uppercase keys
$new_array = array_change_key_case($my_array, CASE_UPPER);
echo "<pre>";
var_dump($new_array);
echo "</pre>";
?>
Browser Output
array(3) { ["NAME"]=> string(8) "John Doe" ["AGE"]=> int(21) ["EMAIL"]=> string(14) "[email protected]" }
Lowercase Example
<?php
$my_array = array("Name" => "John Doe", "Age" => 21, "emAiL" => "[email protected]");
// New array with Lowercase keys
$new_array = array_change_key_case($my_array, CASE_LOWER);
echo "<pre>";
var_dump($new_array);
echo "</pre>";
?>
Browser Output
array(3) { ["name"]=> string(8) "John Doe" ["age"]=> int(21) ["email"]=> string(14) "[email protected]" }
Quick Notes:
How to Convert PHP Array Keys to Uppercase or Lowercase?
- Convert Array Keys to Uppercase
$newArray = array_change_key_case($myArray, CASE_UPPER);
- Convert Array Keys to Lowercase
$newArray = array_change_key_case($myArray, CASE_LOWER);