PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_chunk> <Funcţii ale tablourilor (Array)
Last updated: Fri, 01 Aug 2008

view this page in

array_change_key_case

(PHP 4 >= 4.2.0, PHP 5)

array_change_key_caseModifică toate cheile dintr-un tablou

Descrierea

array array_change_key_case ( array $input [, int $case ] )

Întoarce un tablou cu toate cheile din input transferate în caractere minuscule sau majuscule. Indicii-numere rămân precum sunt.

Parametri

input

Tabloul asupra căruia se lucrează

case

Poate lua valoarea CASE_UPPER sau CASE_LOWER (implicit)

Valorile întroarse

Întoarce un tablou cu cheile transferate în caractere minuscule sau majuscule, sau false dacă input nu este un tablou.

Erori/Excepţii

Aruncă E_WARNING dacă input nu este un tablou.

Exemple

Example #1 Exemplu array_change_key_case()

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

Exemplul de mai sus va afişa:

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

Note

Notă: Dacă un tablou are indici care vor deveni la fel odată ce se va rula această funcţie (de ex. "keY" şi "kEY"), valoarea cea mai de pe urmă din tablou se va suprapune peste ceilalţi indici.



array_chunk> <Funcţii ale tablourilor (Array)
Last updated: Fri, 01 Aug 2008
 
add a note add a note User Contributed Notes
array_change_key_case
andreas dot schuhmacher87 at googlemail dot com
15-Apr-2008 03:01
multibyte and multi-dimensional-array support, have fun!

<?php
    define
('ARRAY_KEY_FC_LOWERCASE', 25); //FOO => fOO
   
define('ARRAY_KEY_FC_UPPERCASE', 20); //foo => Foo
   
define('ARRAY_KEY_UPPERCASE', 15); //foo => FOO
   
define('ARRAY_KEY_LOWERCASE', 10); //FOO => foo
   
define('ARRAY_KEY_USE_MULTIBYTE', true); //use mutlibyte functions
   
    /**
    * change the case of array-keys
    *
    * use: array_change_key_case_ext(array('foo' => 1, 'bar' => 2), ARRAY_KEY_UPPERCASE);
    * result: array('FOO' => 1, 'BAR' => 2)
    *
    * @param    array
    * @param    int
    * @return     array
    */
   
function array_change_key_case_ext(array $array, $case = 10, $useMB = false, $mbEnc = 'UTF-8') {
       
$newArray = array();
       
       
//for more speed define the runtime created functions in the global namespace
       
        //get function
       
if($useMB === false) {
           
$function = 'strToUpper'; //default
           
switch($case) {
               
//first-char-to-lowercase
               
case 25:
                   
//maybee lcfirst is not callable
                   
if(!function_exists('lcfirst'))
                       
$function = create_function('$input', '
                            return strToLower($input[0]) . substr($input, 1, (strLen($input) - 1));
                        '
);
                    else
$function = 'lcfirst';
                    break;
               
               
//first-char-to-uppercase               
               
case 20:
                   
$function = 'ucfirst';
                    break;
               
               
//lowercase
               
case 10:
                   
$function = 'strToLower';
            }
        } else {
           
//create functions for multibyte support
           
switch($case) {
               
//first-char-to-lowercase
               
case 25:
                   
$function = create_function('$input', '
                        return mb_strToLower(mb_substr($input, 0, 1, \''
. $mbEnc . '\')) .
                            mb_substr($input, 1, (mb_strlen($input, \''
. $mbEnc . '\') - 1), \'' . $mbEnc . '\');
                    '
);
                   
                    break;
               
               
//first-char-to-uppercase
               
case 20:
                   
$function = create_function('$input', '
                        return mb_strToUpper(mb_substr($input, 0, 1, \''
. $mbEnc . '\')) .
                            mb_substr($input, 1, (mb_strlen($input, \''
. $mbEnc . '\') - 1), \'' . $mbEnc . '\');
                    '
);
                   
                    break;
               
               
//uppercase
               
case 15:
                   
$function = create_function('$input', '
                        return mb_strToUpper($input, \''
. $mbEnc . '\');
                    '
);
                    break;
               
               
//lowercase
               
default: //case 10:
                   
$function = create_function('$input', '
                        return mb_strToLower($input, \''
. $mbEnc . '\');
                    '
);
            }
        }
       
       
//loop array
       
foreach($array as $key => $value) {
            if(
is_array($value)) //$value is an array, handle keys too
               
$newArray[$function($key)] = array_change_key_case_ex($value, $case, $useMB);
            elseif(
is_string($key))
               
$newArray[$function($key)] = $value;
            else
$newArray[$key] = $value; //$key is not a string
       
} //end loop
       
       
return $newArray;
    }
?>
dot dot dot dot dot alexander at gmail dot com
30-Jan-2008 06:22
Basically this is the function if your version is lower than 4.2.0
<?php
if ( !defined('CASE_LOWER') )define('CASE_LOWER', 0);
if ( !
defined('CASE_UPPER') )define('CASE_UPPER', 1);

if(!
function_exists("array_change_key_case")){
    function
array_change_key_case($input, $case=0){
        if(!
is_array($input))return FALSE;
       
$product = array();
        foreach(
$input as $key => $value){
            if(
$case){ //Upper
               
$key2 = (  (is_string($key)) ? (strtoupper($key)) : ($key)  );
               
$product[$key2] = $value;
            }
            else{
//Lower
               
$key2 = (  (is_string($key)) ? (strtolower($key)) : ($key)  );
               
$product[$key2] = $value;
            }
        }
        return
$product;
    }
/* endfunction array_change_key_case */
}/* endfunction exists array_change_key_case*/
?>
john at doe dot com
26-Sep-2007 09:04
<?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;
}
?>
cm at gameswelt dot de
10-Aug-2007 01:11
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
)
*/
aidan at php dot net
02-Jun-2004 05: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

array_chunk> <Funcţii ale tablourilor (Array)
Last updated: Fri, 01 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites