Stop PHP from creating arrays in $_POST superglobal
forms, html, php, superglobals
Solution
By using the brackets `[]` you explicitly tell PHP to create an array, just don't use the brackets:
<input type="text" name="foo_0" value="x" />
<input type="text" name="foo_1" value="y" />
The fields will be available in the $_POST array as `$_POST['foo_0']` and `$_POST['foo_1']`.
If you don't have influence on the markup (which is weird, since you could always change them client side) you need to flatten the array.
$post = array();
foreach ($_POST as $key => $value) {
if (!is_array($value)) {
$post[$key] = $value;
} else {
foreach ($value as $foo => $item) {
$post[$foo] = $item;
}
}
}
Or read some great input on array flattening.
Problem
PHP will automatically convert ``` <input type="text" name="foo[0]" value="x" /> <input type="text" name="foo[1]" value="y" /> ``` into ``` $_POST['foo'] = array( 0 => 'x', 1 => 'y' ); ``` Which is what you want most of the time. However, in this case I would like this not to happen. Is there anyway to tell PHP to not do this? I realize I could parse `php://input` myself, but I'd rather not do that if I can avoid it. I also don't have the option of renaming the input names.