PHP中数组输入的键不正确

时间:2021-08-13 10:45:12

I'm using a number of HTML input fields to post a set of day-price values from a form to PHP. My minimal HTML looks like this:

我正在使用许多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]">

The key indicates the amount of days (can also include half days etc.), and the values should be the prices for their respective amount of days.

键表示天数(也可包括半天等),值应为各自天数的价格。

However, when I input some values and use var_dump($_POST) to check what gets posted, I get a normal sequential array, containing integers 0-4 as keys, as opposed to the strings '0.25', '0.5' and so on.

但是,当我输入一些值并使用var_dump($ _ POST)来检查发布的内容时,我得到一个正常的顺序数组,包含整数0-4作为键,而不是字符串'0.25','0.5'等等。

Here's some example input and output:

这是输入和输出的一些示例:

Input (not that the values are the same as the keys for convenience):

输入(为方便起见,不是为了与键相同的值):

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

Output ($_POST):

输出($ _POST):

array (size=5)
  0 => string '0.5' (length=3)
  1 => float 1
  2 => float 2
  3 => float 3
  4 => float 4

As you can see, the 0.25 has disappeared and the 0.5 input was mapped to 0. Likely both inputs get mapped to 0 and 0.5 overrides 0.25.

如您所见,0.25已经消失,0.5输入被映射为0.可能两个输入都映射到0和0.5覆盖0.25。

When I check the same input using JS however (console.log($('form').serializeArray());), the results look as expected: Screenshot.

当我使用JS检查相同的输入时(console.log($('form')。serializeArray());),结果看起来像预期的那样:截图。

So my questions are:

所以我的问题是:

  1. Is this intended behavior of PHP?
    1. If so, what is the best workaround?
    2. 如果是这样,最好的解决方法是什么?
    3. If not, what am I doing wrong?
    4. 如果没有,我做错了什么?
  2. 这是PHP的预期行为吗?如果是这样,最好的解决方法是什么?如果没有,我做错了什么?

Thanks!

谢谢!

3 个解决方案

#1


1  

Alright in the end it seems this issue is caused by PHP converting floats (even in string form) to the nearest integer in array keys, as pointed out by Ryan Vincent. This would explain the problem.

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

So I tried to force PHP to use strings by changing the keys to strings like 'test0.25', but that didn't work either. Somewhere between the form getting posted and the $_POST array being constructed, all keys with dots (or commas) in them get lost. A possible solution could be to change the numbers to '0_25' and then replacing the _ with . later on in PHP to get the original numbers. I didn't try this though, as I didn't think it would be a very elegant solution.

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

I solved the problem by simply adding another group of (hidden) input fields with different names to store the keys. My form looks something like this:

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

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

The in PHP I simply match the key-value pairs. To ensure the right matches, one could of course add explicit indices to the input names.

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

This seemed like the best solution to me.

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

#2


0  

It could be related to the PHP version. I tested it on PHP 5.4.43 and it worked exactly as you desired.

它可能与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"
}

The test code producing the above results:

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

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

#3


0  

PHP will accept array syntax as POST field names, however that field will then become an array so you will need to handle it like this: $_POST['prices'][0]

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

Example PHP Code

示例PHP代码

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

Here's the code you posted followed by the output from the PHP example:

这是您发布的代码,后跟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

Here's another way to do arrays with input fields.

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

<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

#1


1  

Alright in the end it seems this issue is caused by PHP converting floats (even in string form) to the nearest integer in array keys, as pointed out by Ryan Vincent. This would explain the problem.

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

So I tried to force PHP to use strings by changing the keys to strings like 'test0.25', but that didn't work either. Somewhere between the form getting posted and the $_POST array being constructed, all keys with dots (or commas) in them get lost. A possible solution could be to change the numbers to '0_25' and then replacing the _ with . later on in PHP to get the original numbers. I didn't try this though, as I didn't think it would be a very elegant solution.

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

I solved the problem by simply adding another group of (hidden) input fields with different names to store the keys. My form looks something like this:

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

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

The in PHP I simply match the key-value pairs. To ensure the right matches, one could of course add explicit indices to the input names.

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

This seemed like the best solution to me.

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

#2


0  

It could be related to the PHP version. I tested it on PHP 5.4.43 and it worked exactly as you desired.

它可能与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"
}

The test code producing the above results:

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

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

#3


0  

PHP will accept array syntax as POST field names, however that field will then become an array so you will need to handle it like this: $_POST['prices'][0]

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

Example PHP Code

示例PHP代码

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

Here's the code you posted followed by the output from the PHP example:

这是您发布的代码,后跟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

Here's another way to do arrays with input fields.

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

<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