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

From: Date: Fri, 10 Aug 2007 11:11:50 +0000
Subject: note 77016 added to function.array-change-key-case
Groups: php.notes 
Request: Send a blank email to php-notes+get-129396@lists.php.net to get a copy of this message
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 ) */ ---- Server IP: 217.13.201.10 Probable Submitter: 88.217.140.31 ---- Manual Page -- http://www.php.net/manual/en/function.array-change-key-case.php Edit -- https://master.php.net/note/edit/77016 Del: integrated -- https://master.php.net/note/delete/77016/integrated Del: useless -- https://master.php.net/note/delete/77016/useless Del: bad code -- https://master.php.net/note/delete/77016/bad+code Del: spam -- https://master.php.net/note/delete/77016/spam Del: non-english -- https://master.php.net/note/delete/77016/non-english Del: in docs -- https://master.php.net/note/delete/77016/in+docs Del: other reasons-- https://master.php.net/note/delete/77016 Reject -- https://master.php.net/note/reject/77016 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129396) next »