Learn array_change_key_case php function in 1 minute

Function name:  array_change_key_case php function.

There are few things which you need to keep in mind before start to understand this tutorial. You should have basic understanding of php arrays, print_r function & foreach

Syntax: array_change_key_case(array, case)
Support: PHP (4>=4.2.0, 5, 7)
Return Type: array_change_key_case php function will always return array.

Conditions:

  • array(neccessary)
  • case(optional)

Description about array_change_key_case php function:

With the help of this function we can change the keys case to upper or lower.

Array: It’s collection of information on which we want to perform our action like changing the keys case.
Case: It’s just a case defined my php, there are only two options available CASE_UPPER, CASE_LOWER. By default, CASE_LOWER is already set.

Tip: array_change_key_case php function, only works on associative array.But, also will not generate errors, if you’ll pass out indexed or integer array.

Example 1:
In this example, we have an array of header_title that contains some information about fictional company. This example will show you, what happens when we don’t put any case.

//When we don't put the case, CASE_UPPER OR CASE_LOWER
$header_title = array('company name' => 'XicTech',  'Service'=>'SEO | Digital marketing | Web Designing');
print_r(array_change_key_case($header_title));

Output:
Array (
[company name] => XicTech
[service] => SEO | Digital marketing | Web Designing
)

The keys are now in lower case right? As I told you, by default case is CASE_LOWER

Example 2:
I am taking same example as above but by passing second parameter case as CASE_UPPER. It’s really simple, what it will do? Let’s check that out.

//When we are putting CASE_UPPER 

$header_title = array('company name' => 'XicTech',  'Service'=>'SEO | Digital marketing | Web Designing');

print_r(array_change_key_case($header_title, CASE_UPPER));

Output:
Array (
[COMPANY NAME] => XicTech
[SERVICE] => SEO | Digital marketing | Web Designing
)

The keys are now in upper case, because we used CASE_UPPER.

Now, the question is how we can use array_change_key_case php function in real world. Let’s do some basic example.

Example 3:
In this example, I have same array as we wrote. That was header_title, but the only difference is I am doing some extra work here like storing header_title upper case version in another array that is header_info.

$header_title = array('company name' => 'XicTech',  'Service'=>'SEO | Digital marketing | Web Designing');
$header_info=array_change_key_case($header_title, CASE_UPPER);

foreach($header_info as $key => $value){
    echo($key." : ". $value . "<br/>");
}

Output:
COMPANY NAME : XicTech
SERVICE : SEO | Digital marketing | Web Designing

We displayed the keys in upper case to show the impact of titles (COMPANY NAME & SERVICE).

Learn array_change_key_case php function from Video Tutorial below:

Don’t forget to tweet it & subscribe for more tutorials:

Best Resource to learn PHP functions with in depth explanation. I just learned array_change_key_case within 1 minute from here Click To Tweet

Let me know if you’ll have issues in code in comments. I’ll wait for your feedback.

Don’t forget to share.

You will also like to learn in_array php function in 1 minute click here to learn this function

Add Comment