Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : array_change_key_case

array_change_key_case

Changes all keys in an array (PHP 4 >= 4.2.0, PHP 5)
array array_change_key_case ( array input [, int case] )

Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.

Parameters

input

The array to work on

case

Either CASE_UPPER or CASE_LOWER (default)

Return Values

Returns an array with its keys lower or uppercased, or false if input is not an array.

Errors/Exceptions

Throws E_WARNING if input is not an array.

Examples

Example 233. array_change_key_case() example

<?php
$input_array
= array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>

The above example will output:

Array
(
   [FIRST] => 1
   [SECOND] => 4
)


Notes

Note:

If an array has indices that will be the same once run through this function (e.g. "keY" and "kEY"), the value that is later in the array will override other indices.

Code Examples / Notes » array_change_key_case

anonymous

To change all values case simply make use of array_flip.
     $array = array_flip($array);
     $array = array_change_key_case($array , CASE_LOWER);
     $array = array_flip($array);


aidan

This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat


felix dot gilcher

Please, someone remove the note below by anonymous. Using
array_flip($array);
array_change_key_case($array);
array_flip($array);
to uppercase/lowercase all values is not only inefficient as hell, but also produces wrong results - it will remove all duplicate values. The proper way to do this is using
array_map() and strtoupper()/strtolower()
thanks


cm

I just changed the code a little bit so you havent got a code that repeats itself.
<?php
function array_change_key_case_secure($array = array(), $case = CASE_UPPER){
$secure = array();
$functionWrap = array(CASE_UPPER => 'strtoupper',
CASE_LOWER => 'strtolower');

foreach($array as $key => $val){
if(isset($functionWrap[$key])){
$key = $functionWrap[$case]($key);
$secure[$key][] = $val;
} else {
die('Not a known Type');
}
}

foreach($secure as $key => $val){
if(count($secure[$key]) == 1){
$secure[$key] = $val[0];
}
}

return $secure;
}
$myArray = array('A' => 'Hello',
'B' => 'World',
'a' => 'how are you?');
print_r($myArray);
$myArray = array_change_key_case_secure($myArray);
print_r($myArray);
/*
Array
(
   [A] => Hello
   [B] => World
   [a] => how are you?
)
Array
(
   [A] => Array
       (
           [0] => Hello
           [1] => how are you?
       )
   [B] => World
)
*/


john

<?php
function array_change_value_case($input, $case = CASE_LOWER)
{
$aRet = array();

if (!is_array($input))
{
return $aRet;
}

foreach ($input as $key => $value)
{
if (is_array($value))
{
$aRet[$key] = array_change_value_case($value, $case);
continue;
}

$aRet[$key] = ($case == CASE_UPPER ? strtoupper($value) : strtolower($value));
}

return $aRet;
}
?>


cdblog

<?php
/**
* @return array
* @author Cocol
* @desc  change the key case , if found repeat keys, then  convert to array
* for more detail please visit http://php.clickz.cn/array/array_change_key_case.html
*/
function array_change_key_case_secure($array = array(),$case = CASE_UPPER) {
$secure = array();
if ($case == CASE_UPPER) {
foreach ($array as $key=>$val) {
$key = strtoupper($key);
if (!array_key_exists($key,$secure)) {
$secure[$key][] = $val;
} else {
$secure[$key][] = $val;
}
}
} else if ($case == CASE_LOWER) {
foreach ($array as $key=>$val) {
$key = strtolower($key);
if (!array_key_exists($key,$secure)) {
$secure[$key][] = $val;
} else {
$secure[$key][] = $val;
}
}
}

foreach ($secure as $key=>$val) {
if (count($secure[$key]) == 1) {
$secure[$key] = $val[0];
}
}
return $secure;
}
$array = array(
'a' => "john",
'A' => "vary",
'c' => "cocol",
);
print_r($array);
$array = array_change_key_case_secure($array,CASE_UPPER);
print_r($array);
?>
output:
Array
(
   [a] => john
   [A] => vary
   [c] => cocol
)
Array
(
   [A] => Array
       (
           [0] => john
           [1] => vary
       )
   [C] => cocol
)


Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker