【发布时间】:2015-12-01 02:00:58
【问题描述】:
我正在使用许多 HTML 输入字段将一组日价值从表单发布到 PHP。我的最小 HTML 如下所示:
<input type="text" name="prices[0.25]">
<input type="text" name="prices[0.5]">
<input type="text" name="prices[1]">
<input type="text" name="prices[2]">
<input type="text" name="prices[3]">
<input type="text" name="prices[4]">
key表示天数(也可以包括半天等),value是对应天数的价格。
但是,当我输入一些值并使用 var_dump($_POST) 检查发布的内容时,我得到一个正常的顺序数组,其中包含整数 0-4 作为键,而不是字符串 '0.25'、'0.5' 等开。
以下是一些输入和输出示例:
输入(不是为了方便起见值与键相同):
<input type="text" name="prices[0.25]" value="0.25">
<input type="text" name="prices[0.5]" value="0.5">
<input type="text" name="prices[1]" value="1">
<input type="text" name="prices[2]" value="2">
<input type="text" name="prices[3]" value="3">
<input type="text" name="prices[4]" value="4">
输出($_POST):
array (size=5)
0 => string '0.5' (length=3)
1 => float 1
2 => float 2
3 => float 3
4 => float 4
如您所见,0.25 已经消失,0.5 输入被映射到 0。很可能两个输入都被映射到 0 并且 0.5 会覆盖 0.25。
然而,当我使用 JS (console.log($('form').serializeArray());) 检查相同的输入时,结果看起来如预期:Screenshot。
所以我的问题是:
- 这是 PHP 的预期行为吗?
- 如果是这样,最好的解决方法是什么?
- 如果不是,我做错了什么?
谢谢!
【问题讨论】:
-
为什么 1-4 被列为浮点数?
-
@JoshJ 我不知道。
-
@JohnDoe 那是我已经知道的东西,但它对我的问题没有帮助。
-
数组不能将浮点数作为键,因为它们将被转换为整数。见Additionally the following key casts will occur。我怀疑在处理 PHP 的某个地方将它们视为数字而不是字符串。