Sunday, October 18, 2020

PHP array_change_key_case() Function



Hello folks! welcome to another section of our tutorial on PHP. In this tutorial guide, we are going to be discussing about PHP function array_change_key_case().

The PHP array_change_key_case() function changes the case of all keys of the passed arrays and it then returns an array with all the keys either in lowercase or in an 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] )


Parameter Details

Sr.NoParameter & 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

It 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 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's 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
)


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 display the following 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


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 i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.