开发者

Non-English characters as array key - "undefined index"

开发者 https://www.devze.com 2023-03-24 05:45 出处:网络
I have an array of names which I am reading in from a mysql table. Everything works fine, except I\'ve just noticed that if the name contains any non-English characters, e.g. Scandanavian, Greek etc.

I have an array of names which I am reading in from a mysql table. Everything works fine, except I've just noticed that if the name contains any non-English characters, e.g. Scandanavian, Greek etc. this causes an error, "undefined index".

What I don't get though is that the names are being listed out fine in another part of my app, using the same function. The only difference being that I am passing the name specifically as a parameter when this error occurs. Other times I am just iterating through the whole list and echoing out all the names.

Each name is the array key, I'm getting the error on the line for the else return:

   if($nameParam ==""){ 
         return $listOfNames;
   }
   else{ 
         return $listOfNames[$nameParam];   
   }

Any ideas how I could fix this?

Would it be better to somehow check each name and apply some kind of encoding/dec开发者_高级运维oding writing/reading to the mysql table? If so what would be the best way to do this? Thanks..


I've looked through PHP C code for if PHP does modifies/strips unicode chars in array key strings in any way but couldn't find any conversion logic there. Just to confirm this I've asked a question on php.net mailing list:

Q: ....blah blah...I was just wondering - when unicode characteres are used in array key - does PHP strips them or affects them in any way? or does PHP simply treats keys as binary string, the end of the story?

A: I believe that string array keys are treated as binary strings, and never modified. I couldn't find you a source, but I've always worked on this assumption and never hit a problem. UTF-8 keys definitely work fine (*); I'm also not aware of any max length the keys can have (meaning you shouldn't have to worry about a multi-byte character getting truncated in the middle).

Thus I now believe that having whatever UTF8 characters in array keys shouldn't affect your ability to use arrays as usual.

ALSO

I'm sure you already aware of this but I would check that db uses correct encoding to store data. In mysql I always use UTF-8 charset and utf8_general_ci collation.

0

精彩评论

暂无评论...
验证码 换一张
取 消