note 82522 added to function.array-change-key-case

From: Date: Tue, 15 Apr 2008 01:01:31 +0000
Subject: note 82522 added to function.array-change-key-case
Groups: php.notes 
Request: Send a blank email to php-notes+get-138315@lists.php.net to get a copy of this message
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; } ?> ---- Server IP: 217.13.201.10 Probable Submitter: 84.152.110.30 ---- Manual Page -- http://www.php.net/manual/en/function.array-change-key-case.php Edit -- https://master.php.net/note/edit/82522 Del: integrated -- https://master.php.net/note/delete/82522/integrated Del: useless -- https://master.php.net/note/delete/82522/useless Del: bad code -- https://master.php.net/note/delete/82522/bad+code Del: spam -- https://master.php.net/note/delete/82522/spam Del: non-english -- https://master.php.net/note/delete/82522/non-english Del: in docs -- https://master.php.net/note/delete/82522/in+docs Del: other reasons-- https://master.php.net/note/delete/82522 Reject -- https://master.php.net/note/reject/82522 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138315) next »