note 64440 modified in function.array-change-key-case by bjori

From: Date: Fri, 14 Apr 2006 13:26:11 +0000
Subject: note 64440 modified in function.array-change-key-case by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-108070@lists.php.net to get a copy of this message
<?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; } ?> --was-- My previous note created an infinite loop in that it kept adding to the array so each() never failed. Please delete my previous note, thanks. <?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; } ?> http://php.net/manual/en/function.array-change-key-case.php

« previous php.notes (#108070) next »