2

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:

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

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):

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.

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

So my questions are:

  1. Is this intended behavior of PHP?
    1. If so, what is the best workaround?
    2. If not, what am I doing wrong?

Thanks!

8
  • Why are 1-4 being listed as float? Commented Dec 1, 2015 at 2:06
  • stackoverflow.com/questions/14050812/… Commented Dec 1, 2015 at 2:09
  • @JoshJ I have no idea. Commented Dec 1, 2015 at 2:18
  • @JohnDoe That's the stuff I already know, but it doesn't help with my question. Commented Dec 1, 2015 at 2:19
  • 1
    Arrays cannot have floats as keys as they will be cast to integers. see Additionally the following key casts will occur. I suspect that somewhere in the processing PHP treats them as numbers and not strings. Commented Dec 1, 2015 at 2:41

3 Answers 3

1

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

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

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.

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.

This seemed like the best solution to me.

Comments

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]

Example PHP Code

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:

<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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.