获取发布变量的名称 [英] get name of a post variable

查看:108
本文介绍了获取发布变量的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
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.com

name : Simo Taqi
email : example@ymail.com

换句话说:

如果我有一个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
    • 这篇关于获取发布变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆