我有一个由客户请求表填充的HTML表单。用户查看每个请求,设置操作并提交。该操作被设置为开机自检参数,如下所示
$postArray = array_keys($_POST);
[1]=>Keep [2]=>Reject [3]=>Reject [4]=>Ignore ["item id"]=>"operation"
这就是我解析我的 POST 参数的方式,看起来很尴尬,未使用的$idx
.我是PHP的新手,有没有更流畅的方法?
$postArray = array_keys($_POST);
foreach($postArray as $idx => $itemId) {
$operation = $_POST[$itemId];
echo "$itemId $operation </br>";
// ...perform operation...
}
foreach ($_POST as $key => $value) // $key will contain the name of the array key
您可以使用
foreach($_POST as $itemId => $operation ) {
echo "$itemId $operation </br>";
// ...perform operation...
}
相反
http://nz.php.net/manual/en/control-structures.foreach.php
如果不需要,您不必在循环中使用$idx。可能是这样的:
foreach($postArray as $itemId) {
...
}
主要问题是数据结构非常混乱。也许这是组织表单输出的更好方法。可能在某些结构良好的关联数组中。我看不到表格,我不知道细节,所以很难说更多。