【问题标题】:Incorrect keys for array inputs in PHPPHP中数组输入的键不正确
【发布时间】: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

所以我的问题是:

  1. 这是 PHP 的预期行为吗?
    1. 如果是这样,最好的解决方法是什么?
    2. 如果不是,我做错了什么?

谢谢!

【问题讨论】:

标签: php html input


【解决方案1】:

可能与 PHP 版本有关。我在 PHP 5.4.43 上对其进行了测试,结果完全符合您的要求。

array(2) {
  ["prices"]=>
  array(6) {
    ["0.25"]=>
    string(3) "0.5"
    ["0.5"]=>
    string(3) "0.9"
    [1]=>
    string(3) "1.5"
    [2]=>
    string(1) "2"
    [3]=>
    string(1) "4"
    [4]=>
    string(1) "6"
  }
  ["submit"]=>
  string(6) "submit"
}

产生上述结果的测试代码:

<?php
ob_start();
var_dump($_POST);
$dump=ob_get_clean();
echo('<pre>'.$dump.'</pre>');
?>
<form name='test' method='post' action='test.php'>
<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]">
<input type="submit" name="submit" value="submit">
</form>

【讨论】:

    【解决方案2】:

    好吧,最后看来这个问题是由 PHP 将浮点数(即使是字符串形式)转换为数组键中最接近的整数引起的,正如 Ryan Vincent 所指出的那样。这可以解释问题。

    所以我试图通过将键更改为 'test0.25' 之类的字符串来强制 PHP 使用字符串,但这也不起作用。在发布表单和构造 $_POST 数组之间的某个位置,所有带有点(或逗号)的键都会丢失。一种可能的解决方案是将数字更改为 '0_25',然后稍后在 PHP 中将 _ 替换为 . 以获得原始数字。不过我没有尝试过,因为我认为这不是一个非常优雅的解决方案。

    我通过简单地添加另一组具有不同名称的(隐藏)输入字段来存储键来解决了这个问题。我的表单看起来像这样:

    <input type="text" name="prices[]">
    <input type="text" name="prices[]">
    <input type="text" name="prices[]">
    <input type="text" name="prices[]">
    <input type="text" name="prices[]">
    <input type="text" name="prices[]">
    
    <input type="text" name="days[]" value="0.25">
    <input type="text" name="days[]" value="0.5">
    <input type="text" name="days[]" value="1">
    <input type="text" name="days[]" value="2">
    <input type="text" name="days[]" value="3">
    <input type="text" name="days[]" value="4">
    

    在 PHP 中我只是简单地匹配键值对。为了确保正确匹配,当然可以为输入名称添加显式索引。

    这对我来说似乎是最好的解决方案。

    【讨论】:

      【解决方案3】:

      PHP 将接受数组语法作为 POST 字段名称,但是该字段随后将变为数组,因此您需要像这样处理它:$_POST['prices'][0]

      示例 PHP 代码

      foreach ( $_POST['prices'] as $key => $value ) {
          echo "price{$key}: {$value}<br/>";
      }
      

      这是您发布的代码,后面是 PHP 示例的输出:

      <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">
      

      price[0.25]: 0.25
      price[0.5]: 0.5
      price[1]: 1
      price[2]: 2
      price[3]: 3
      price[4]: 4
      

      这是使用输入字段创建数组的另一种方法。

      <input type="text" name="prices[]" value="0.25">
      <input type="text" name="prices[]" value="0.5">
      <input type="text" name="prices[]" value="1">
      <input type="text" name="prices[]" value="2">
      <input type="text" name="prices[]" value="3">
      <input type="text" name="prices[]" value="4">
      

      price[0]: 0.25
      price[1]: 0.5
      price[2]: 1
      price[3]: 2
      price[4]: 3
      price[5]: 4
      

      【讨论】:

        猜你喜欢
        • 2018-01-13
        • 2020-03-01
        • 1970-01-01
        • 2019-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多