Sometime we need an array form elements like this

1
2
3
4
<input type="text" name="foo[]" value="a" />
<input type="text" name="foo[3]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[7]" value="d" />

But some of that contain key some didn’t. What will we actually get in php $_POST?

According to example above,

1
2
3
4
<?php
echo '<pre>';
print_r($_POST['foo']);
echo '</pre>';

will output:

1
2
3
4
5
6
7
Array
(
[0] => a
[3] => b
[4] => c
[7] => d
)

But let say the index is in string form

1
2
3
4
<input type="text" name="foo[]" value="a" />
<input type="text" name="foo['3']" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo['7']" value="d" />

will output:

1
2
3
4
5
6
7
Array
(
[0] => a
['3'] => b
[1] => c
['7'] => d
)