New function: array_change_key_case()

From: Date: Mon, 24 Sep 2001 01:20:42 +0000
Subject: New function: array_change_key_case()
Groups: php.dev 
Request: Send a blank email to php-dev+get-66439@lists.php.net to get a copy of this message
Problem: I was faced with a problem of porting a large PHP application that was originally written for Oracle to Postgres. The problem is that Oracle always returns column names in uppercase, while Postgres does exactly the opposite. So all the code that was handling rows returned from the database as associative arrays did not work due to the case sensitivity of the array keys. During the discussion on pear-dev it became apparent that was very difficult to achieve the code portability in PEAR::DB for the same reason. Attempt to write a PHP userspace function that will change the case of array keys was benchmarked which produced very poor result. Passing each row returned from database backed through that function resulted in performance loss of 110-120%. Solution: There is a need for a function that will lowecase [or uppercase] all keys in the associative array. After implementation and benchmarking the result showed some ~15% performance loss which is much more acceptable than the original ~120%. Function prototype: array_change_key_case(array input [, int case=CASE_LOWER|CASE_UPPER]) /* Retuns an array with all string keys lowercased [or uppercased] */ Test case: $a=array("Ab"=>"test1", "AB"=>"test2", "ac"=>3, 1=>"test3"); $b=array_change_key_case($a, CASE_LOWER); var_dump($a); var_dump($b); Test result: array(4) { ["Ab"]=> string(5) "test1" ["AB"]=> string(5) "test2" ["ac"]=> int(3) [1]=> string(5) "test3" } array(3) { ["ab"]=> string(5) "test2" ["ac"]=> int(3) [1]=> string(5) "test3" } If there are no objections I would ask someone with CVS account to apply the attached patch. Edin

Attachment: [application/x-gzip] array.patch.gz
« previous php.dev (#66439) next »