CNBorn CNBorn - 2 months ago 24
PHP Question

PHP Array Key encoding?

I use a encoded string as a key in array, and also uses the same string as a value in the array, like below code indicates:

$string = 'something in some encode';
$list = array();
$list[$string]['name'] = $string;


when I print_r the array out(just print_r without headers/encoding specific), found that the key in the array and it's 'name' value are not as printed as a same string, it seems to have different encoding.

I'm doing this with chinese character. In php.ini I don't have specific encoding line(Don't know whether it has anything to do with this).

Is there anything about the string encoding in Array keys? Or just I got them in a wrong way? Thanks for your help.

Answer

I tried in Japanese (as is what I can test):

$test["要"]["name"] = "要";
print_r($test);

And the result went fine, as expected. I'm using UTF-8 for everything. I'm not sure if its a problem with your encoding settings (in php.ini) or the encoding you are using. if that is a problem, why don't you try to encode it with base64? (or other Ascii encoder). That way would be something like:

$test["6KaB"]["name"] = "要";

I'm not sure what is your goal, so let me know if it was useful.