array_change_key_case

(PHP 4 >= 4.2.0, PHP 5)

array_change_key_case -- 返回字符串键名全为小写或大写的数组

说明

array array_change_key_case ( array input [, int case] )

array_change_key_case()input 数组中的所有键名改为全小写或大写。改变是根据后一个选项 case 参数来进行的。可以在这里用两个常量,CASE_UPPERCASE_LOWER。默认值是 CASE_LOWER。本函数不改变数字索引。

例子 1. array_change_key_case() 例子

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

上例将输出:

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

如果一个数组中的多个键名经过本函数后变成一样的话(例如 "keY" 和 "kEY"),最后一个值将覆盖其它的值。


add a note add a note User Contributed Notes
egingell at sisna dot com
14-Apr-2006 09:01
<?php

/**
 * Take an array and make and change the case according to $mode
 * bool array_key_case_change( array &array [, int mode ] )
 *
 * array is the input array.
 *
 * mode can be one of three choices: CASE_UPPER, CASE_LOWER, CASE_FIRST,
 * defaults to CASE_FIRST if left out.
 *
 * Returns TRUE on success, FALSE on failure.
 *
 * Numeric keys will not change since there is no capital 2 (unless you work in tech support).
 *
 * The following IF statements will make sure that the required constants are set
 **/
if (!defined('CASE_UPPER')) define('CASE_UPPER', 1);
if (!
defined('CASE_LOWER')) define('CASE_LOWER', 2);
if (!
defined('CASE_FIRST')) define('CASE_FIRST', 3);

function
array_key_case_change(&$array, $mode = CASE_FIRST) {

  
// Make sure $array is really an array
  
if (!is_array($array)) return false;
  
  
$temp = $array;
   while (list(
$key, $value) = each($temp)) {
      
// First we unset the original so it's not lingering about
      
unset($array[$key]);

      
// Then modify the $key
      
switch($mode) {
           case
CASE_UPPER:
              
$key = strtoupper($key);
               break;
           case
CASE_LOWER:
              
$key = strtolower($key);
               break;
           case
CASE_FIRST:
              
$key = ucfirst(strtolower($key));
               break;
       }

      
// Lastly readd to the array using the new $key
      
$array[$key] = $value;
   }
   return
true;
}

?>
cmorell184 at comcast dot net
16-Jan-2006 02:59
The following function will do a switch case case on your array keys (foobar would become FoObAr)
Not sure if it will come in handy, but it may
<?php
function array_case_switch($array) {
  
$curr = 1;
   foreach (
$array as $key=>$value) {
      
$newkey = "";
       for (
$i = 0; $i < strlen($key); $i++) {
           if (
$curr % 2) {
              
$newkey .= ($i % 2) ? strtolower(substr($key,$i,1)) : strtoupper(substr($key,$i,1));
           } else {
              
$newkey .= ($i % 2) ? strtoupper(substr($key,$i,1)) : strtolower(substr($key,$i,1));
           }
       }
      
$newarray[$newkey] = $value;
      
$curr++;
   }
   return
$newarray;
}
?>
cdblog at gmail dot com
20-May-2005 04:55
<?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
)
benles at bldigital dot com
06-Mar-2005 09:20
In case you want to find the original case of a key:

$arr = Array( "a"=>1, "B"=>2);

function array_get_key_case($arr, $k)
{
   $s = strtolower($k);
   foreach (array_keys($arr) as $key) if (strtolower($key) == $s) return $key;
  return $k;
}

// Should print aBC
echo array_get_key_case($arr, "A");
echo array_get_key_case($arr, "B");
echo array_get_key_case($arr, "C");
aidan at php dot net
02-Jun-2004 11:06
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
euphoria{AT}netdoor.org
18-Nov-2002 01:31
Just a note, this function kills any name-clashes!  Say you had:
$array = array('key1' => 'abc', 'key2' => 'def', 'KEY1' => 'ghi');
$array = array_change_key_case($array);

then you would end up with:
Array
(
   [key1] => ghi
   [key2] => def
)

(Change .org to .com to e-mail me)
Armond