可能重复: PHP $ _POST打印变量名称以及值
Possible Duplicate: PHP $_POST print variable name along with value
我有一个表单(无论字段数如何)..该表单将发送$ _POST数据..我想返回每个$ _POST变量值和名称.
I have a form (whatever number of fields).. this form will send $_POST data.. I want to return every $_POST variable value and name.
我想做这样的事情:
foreach($_POST as $field){ //****some code*****// }以一种显示如下字段和值的方式:
in a way that will display the fields and values like this:
名称:Simo Taqi 电子邮件:example@ymail
name : Simo Taqi email : example@ymail
换句话说:
如果我有一个post变量:$ _POST ['city'] ='拉斯维加斯'
if I have a post variable : $_POST['city']='las vegas'
我想知道如何获取变量的名称:"city".
I want to know how to get the name of the variable : 'city'.
推荐答案$_POST被填充为关联数组,因此您可以执行以下操作:
$_POST is populated as an associative array, so you can just do this:
foreach ($_POST as $name => $val) { echo htmlspecialchars($name . ': ' . $val) . "\n"; }此外,如果您只对字段名称感兴趣,则可以使用array_keys($_POST);返回$_POST中使用的所有键的数组.您可以使用这些键来引用$_POST中的值.
Additionally, if you're just interested in the field names, you can use array_keys($_POST); to return an array of all the keys used in $_POST. You can use those keys to reference values in $_POST.
foreach (array_keys($_POST) as $field) { echo $_POST[$field]; }
- foreach文档
- array_keys文档
- foreach documentation
- array_keys documentation
更多推荐
获取发布变量的名称
发布评论