Given:
<input type="text" name="foo[]" />
<input type="text" name="foo[]" />
etc...
in your form, you'd loop over them with
foreach($_POST['foo'] as $index => $value) {
...
}
The []
in the field name will be stripped off by PHP and used as a hint that it should expect multiple values with the same name, causing it to create a sub-array inside $_GET/$_POST to accomodate those extra values.
You can also suggest which array keys PHP should use, e.g.
<input type="text" name="foo[1]" value="hi there" />
<input type="text" name="foo[abc]" value="TGIF!" />
echo $_POST['foo'][1]; // outputs "hi there"
echo $_POST['foo']['abc'] // outputs "TGIF!"
Multi-dimensional arrays are also supported, using the same notation/access methods.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…