3

The following input will produce $_POST['test']['a'] === "1"

<input name="test[a]" value="1">

The following input will produce $_POST['test'][0] === "2"

<input name="test[]" value="2">

The following input will produce $_POST['test'][0] === "3"

<input name="test[ ]" value="3"> <!-- space in between the [ ] -->

The following input will produce $_POST['test'][' '] === "4"

<input name="test[ ]" value="4"> <!-- two spaces in between the [  ] -->

(I expected white space was ignored or it wasn't, but the examples 3 and 4 shows that only one space is ignored.. more than that will cause all spaces to be accepted).

The following input will produce $_POST['test']["''"] === "5" (note this is a string containing two single quotation marks, this key is not an empty string).

<input name="test['']" value="5">

Is there some possible syntax that will produce $_POST['test'][''] === "4" from a pure HTML input posted to PHP?

Community
  • 1
  • 1
Frank Forte
  • 2,031
  • 20
  • 19
  • Did you try null? https://stackoverflow.com/a/6961339/2943403 – mickmackusa Apr 29 '22 at 16:42
  • Just to clarify, you're trying to get the array key for the 2nd dimension to be an empty string? – parttimeturtle Apr 29 '22 at 17:02
  • 1
    That's maybe not possible. I read these doc [1](https://www.php.net/manual/en/faq.html.php#faq.html.arrays), [2](https://www.php.net/manual/en/language.variables.external.php) but couldn't found the way to set empty string as array key in `$_POST`. The `[]` will be number or indexed array while **anything** inside square bracket will becomes associative array key. So, `['']` will be `array('\'\'' => 'value')`, or `[.]` will be `array('.' => 'value')` and so on. Even `null` character (``) actually is string so it won't work. – vee Apr 29 '22 at 17:07
  • 1
    You may need to set something special as key and replace it with empty string later. For example `name="test[..]"` and replace array key `array('..' => 'value')` with `array('' => 'value')`. – vee Apr 29 '22 at 17:11
  • I am likely going to go with two spaces, then replace them on the back end (for instance, I can just `trim()` each key and it would give the appropriate key, but this means I would have to replace blank with two spaces when outputting the HTML, and add some comment about this hack. – Frank Forte Apr 30 '22 at 17:14

0 Answers0