Hello folks! welcome back to a new section of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP array_change_key_case() Function.
The PHP array_change_key_case() function changes the case of all keys of the passed arrays and returns an array with all the keys either in lowercase or in uppercase based on the option passed.
By default, this function returns lowercased keys.
The PHP array_change_key_case() function changes the case of all keys of the passed arrays and returns an array with all the keys either in lowercase or in uppercase based on the option passed.
By default, this function returns lowercased keys.
Syntax
Following below is the syntax to use this function -
array array_change_key_case ( array $input [, int $case] )
READ: PHP array() Function
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | input (Mandatory) This is the array for which you want to change the case of all the keys. |
2 | case (Optional) This will take constant value either CASE_UPPER or CASE_LOWER. If you do not pass this value then function will change the keys to lower case. |
Return Value
This function returns an array with it's keys either in lowercase or uppercase or False if passed input is not a valid PHP array.
PHP Version
This PHP function was first lunched as part of core PHP v 4.2.0.
Example1
The below example converts all the keys into uppercase -
<?php $input = array("FirSt"=> 10, "SecOnd" => 400, "Third" => 800, ); print_r(array_change_key_case($input, CASE_UPPER)); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [FIRST] => 10 [SECOND] => 400 [THIRD] => 800 )
Example2
The below example converts all the keys into lowercase -
<?php $input = array("FirSt"=> 10, "SecOnd" => 400, "Third" => 800, ); print_r(array_change_key_case($input, CASE_LOWER)); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [first] => 10 [second] => 400 [third] => 800 )
Example3
Let us now check how the default case will if we do not pass the second option in the function -
<?php $input = array("FirSt"=> 10, "SecOnd" => 400, "Third" => 800, ); print_r(array_change_key_case($input)); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [first] => 10 [second] => 400 [third] => 800 )
READ: Top PHP Frameworks
Example4
Following example returns False and raises a warning message because we are trying to pass a simple PHP string instead of the PHP array -
<?php $input = "This is a string"; print_r(array_change_key_case($input, CASE_LOWER)); ?>
Output
This does not produce any result, rather it is going to show the below warning message, and then if you check function return value then it will be False -
PHP Warning: array_change_key_case() expects parameter 1 to be array, string given in main.php on line 3
READ: Design Patterns in PHP
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP array_chunk() Function.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.