array_flip() 和 utf8
我有一个数组(18 个键):
$en = array(
'Ą' => 'A',
'Č' => 'C',
'Ę' => 'E',
'Ė' => 'E',
'Į' => 'I',
'Š' => 'S',
'Ų' => 'U',
'Ū' => 'U',
'Ž' => 'Z',
'ą' => 'a',
'č' => 'c',
'ę' => 'e',
'ė' => 'e',
'į' => 'i',
'š' => 's',
'ų' => 'u',
'ū' => 'u',
'ž' => 'z',
);
这些键是立陶宛符号(utf8 编码)。 当我执行 $lt = array_flip($en);
时,它返回以下内容:
Array
(
[A] => Ą
[C] => Č
[E] => Ė
[I] => Į
[S] => Š
[U] => Ū
[Z] => Ž
[a] => ą
[c] => č
[e] => ė
[i] => į
[s] => š
[u] => ū
[z] => ž
)
如您所见,缺少 4 个键('Ę'、'Ų'、'ę'、'ų')。这是怎么回事?
I have an array (18 keys):
$en = array(
'Ą' => 'A',
'Č' => 'C',
'Ę' => 'E',
'Ė' => 'E',
'Į' => 'I',
'Š' => 'S',
'Ų' => 'U',
'Ū' => 'U',
'Ž' => 'Z',
'ą' => 'a',
'č' => 'c',
'ę' => 'e',
'ė' => 'e',
'į' => 'i',
'š' => 's',
'ų' => 'u',
'ū' => 'u',
'ž' => 'z',
);
those keys are lithuanian simbols (utf8 encoding).
When I do $lt = array_flip($en);
it returns the following:
Array
(
[A] => Ą
[C] => Č
[E] => Ė
[I] => Į
[S] => Š
[U] => Ū
[Z] => Ž
[a] => ą
[c] => č
[e] => ė
[i] => į
[s] => š
[u] => ū
[z] => ž
)
as you can see 4 keys are missing ('Ę', 'Ų', 'ę', 'ų'). Whats going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里与 UTF-8 无关;只是简单的数组/哈希表机制。
这些键:
对应于成对的相同值。当您翻转数组时,每对中的下一个会覆盖前一个,因为单个数组中不能有两个相同的键。因此结果是:
如果您需要像这样将英文字母映射到立陶宛语字母,您可能必须编写一个自定义函数,将多个立陶宛语字母放入数组中,对应于相同的英文字母键(或更确切地说,值)。
Nothing to do with UTF-8 here; just simple array/hashtable mechanics.
These keys:
Correspond to identical values in pairs. When you flip your array, the next of each pair overrides the previous, because you can't have two same keys in a single array. Hence the result:
If you need to map English letters to Lithuanian letters like this you may have to write a custom function that places multiple Lithuanian letters in an array, corresponding to the same English letter key (or rather, value).