PHP: accessing sequentially numbered POST entries
I'm going to have html form that is going to send out chunks of text labeled 'chunk1', 'chunk2', 'chunk3' up to a potentially unknown number. Is there a way to sequentially开发者_运维技巧 go through $_POST['chunk1'] and so on until one isn't set? I could do a foreach but the foreach is going to contain other POST values too.
how about:
for ( $i=1 ; isset($_POST['chunk'.$i]) ; $i++) {
// Do your stuff
}
just use
<input name="chunk[0]">
<input name="chunk[1]">
...
and in PHP then
$_POST['chunk'][$i] ....
Just do your foreach, and check each key entry to make sure it starts with 'chunk'
.
foreach ($_POST as $key => $val) {
if (strstr($key,"chunk") === false) continue;
//do stuff
}
$chunks = preg_grep('/^chunk\d+$/', array_keys($_POST));
this'll return an array of just the POST entries that are chunk1, chunk2, etc...
精彩评论