note 52997 deleted from function.array-change-key-case by nlopess

From: Date: Sat, 24 Nov 2007 11:30:34 +0000
Subject: note 52997 deleted from function.array-change-key-case by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-132531@lists.php.net to get a copy of this message
Note Submitter: cdblog at gmail dot com ---- <?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 )

« previous php.notes (#132531) next »