Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

array_change_key_case()

Example

Convert all keys in an array to uppercase:

<?php
$age=array(“Peter”=>“35”,“Ben”=>“37”,“Joe”=>“43”);
print_r(array_change_key_case($age,CASE_UPPER));
?>

Definition and Usage

The array_change_key_case() function modifies all keys in an array to either lowercase or uppercase.

Syntax

array_change_key_case(array, case)

Parameter Values

Parameter

Description

array

Required. Specifies the array to use

case

Optional. Possible values:

  • CASE_LOWER: The default value, which converts the keys to lowercase.
  • CASE_UPPER: Converts the keys to uppercase.

Technical Details

Return Value:

Returns an array with its keys converted to lowercase or uppercase, or FALSE if the input is not an array.

PHP Version:

4.2+

More Examples

Example

Convert all keys in an array to lowercase:

<?php
$age=array(“Peter”=>“35”,“Ben”=>“37”,“Joe”=>“43”);
print_r(array_change_key_case($age,CASE_LOWER));
?>

Example

If two or more keys result in the same value after using array_change_key_case() (e.g., “b” and “B”), the most recent key-value pair will override the others.

<?php
$pets=array(“a”=>“Cat”,“B”=>“Dog”,“c”=>“Horse”,“b”=>“Bird”);
print_r(array_change_key_case($pets,CASE_UPPER));
?>