2

Am using DB2 with my PHP. DB2 returns all column names in UPPERCASE. If I use db2_fetch_assoc, then the result would be like this

Array { [USERID] => 4 }

But in my php file, the array values assigned like this and its in camelstyle

$this->userId = $row['userId'];

So it throws the error Error : Undefined index userId

I can use array_change_key_case() function to convert the array to lowercase. But I have hundreds of files which get and assign the array values.

Its not possible to change in all of my files. Also its linux machine.

So is there any function available in php to disable the case sensitivity of array keys.

Coder
  • 133
  • 1
  • 5
  • 11
  • I can convert the array keys to lowercase. But again I want to change the keys in this line $this->userId = $row['userId']; in all of my files. – Coder Jun 29 '11 at 06:15

3 Answers3

3

You can extend ArrayObject from the SPL. This way at least you only have to "touch" the functions/methods that get the data from the database.

$row = getRow();
echo $row['userId'];

function getRow() {
    $row = array ( 'USERID' => 4, 'FOO'=>123, 'BAR'=>456 );
    return new UCArrayObject($row);
}

class UCArrayObject extends ArrayObject
{
    public function offsetExists ($index) { return parent::offsetExists(strtoupper($index)); }
    public function offsetGet($index) { return parent::offsetGet(strtoupper($index)); }
    public function offsetSet ($index, $newval) { parent::offsetSet(strtoupper($index), $newval); }
    public function offsetUnset ($index) { parent::offsetUnset(strtoupper($index)); }
}
VolkerK
  • 95,432
  • 20
  • 163
  • 226
1

I don't know about DB2, but most libraries will return the rows in the case you specify them in your query, so, you can SELECT USERID, SELECT userid or SELECT UserId, whichever suits you best.

The keys of an array are not case insensitive ever.

It would be possible to write a class that mimics an array (look on PHP.net. you'll have to implement an interface to achieve this). Every value you add to it, can be added to an internal array structure using a lowercase version of the key. When reading a value, you can also use the lowercase version of the specified key. This will essentially make your class behave like a case insensitive array.

A class like this can be used in for-loops too, but unfortunately it cannot be passed to functions that actually require an array parameter.

A different solution: If you have a fairly small amount of queries, and your database layer is nicely separated from the rest of your application, you can apply some sort of translation. Just iterate through the resulting arrays, and map their keys to a specific casing you like, using a translation array. E.g. make an array containing ('lowercase'=>'CamelCase') versions of your field names. That way, you can easily find names and find the right casing for them. Using the found keys, you can construct a new array, before returning it to the requesting code.

Those keys still won't be case insensitive, but they will have the casing you want.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
0

No, there isn't. PHP just don't do that. Array case has to be swapped out on a case-by-case basis.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166