与值一起打印$ _ POST变量名 [英] Print $_POST variable name along with value

查看:287
本文介绍了与值一起打印$ _ POST变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中的POST对此我并不总是知道该变量的字段,我会处理的名字。

I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.

我有一个功能,将通过循环值(但是我也想捕捉连同它的变量名。)

I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)

foreach ($_POST as $entry)
{
     print $entry . "<br>";
}

在我弄清楚如何抓住变量名,我还需要弄清楚我怎样才能使通过阵列功能足够聪明来检测和循环的变量,如果他们是present(即,如果我有一些复选框值。)

Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)

推荐答案

如果你只是想打印整个$ _POST数组验证您的数据被正确发送,使用的print_r

If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:

print_r($_POST);

要递归打印数组的内容:

To recursively print the contents of an array:

printArray($_POST);

function printArray($array){
     foreach ($array as $key => $value){
        echo "$key => $value";
        if(is_array($value)){ //If $value is an array, print it as well!
            printArray($value);
        }  
    } 
}

涂一些填充,以嵌套的数组:

Apply some padding to nested arrays:

printArray($_POST);

/*
 * $pad='' gives $pad a default value, meaning we don't have 
 * to pass printArray a value for it if we don't want to if we're
 * happy with the given default value (no padding)
 */
function printArray($array, $pad=''){
     foreach ($array as $key => $value){
        echo $pad . "$key => $value";
        if(is_array($value)){
            printArray($value, $pad.' ');
        }  
    } 
}

is_array 如果给定的变量是一个数组返回true。

is_array returns true if the given variable is an array.

您也可以使用array_keys将返回所有的字符串名称。

You can also use array_keys which will return all the string names.

这篇关于与值一起打印$ _ POST变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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