使用 http_build_query() 时编码的数组键

array keys encoded when using http_build_query()

使用 http_build_query() 将数组编码为 URL 会在数组键也是 html-char 代码时产生奇怪的行为。

例如:

return http_build_query([
   'id' = > ['my', 'data', 'here'], // no problem
   'class' = > ['my', 'data', 'here'], // no problem
   'yen' = > ['my', 'data', 'here'], // ¥ html car is ¥
   'parameter' = > ['my', 'data', 'here'], // ¶ html char is ¶
]);

编码结果为:

id[0]=my&id[1]=data&id[2]=here&class[0]=my&class[1]=data&class[2]=here¥[0]=my¥[1]=data¥[2]=here¶meter[0]=my¶meter[1]=data¶meter[2]=here

这是怎么回事,我不可能不能将单词参数用作数组键。

如果您查看 HTML 输出的来源,您将看到

id%5B0%5D=my&id%5B1%5D=data&id%5B2%5D=here&class%5B0%5D=my&class%5B1%5D=data&class%5B2%5D=here&yen%5B0%5D=my&yen%5B1%5D=data&yen%5B2%5D=here&parameter%5B0%5D=my&parameter%5B1%5D=data&parameter%5B2%5D=here

这是正确的。仅显示时,浏览器会将 &yen 等格式错误的实体解释为 ¥。服务器端完全不用担心。

HTML 个实体 reference

演示: IDEOne