打印$ _POST变量名称以及值

时间:2022-06-28 23:20:07

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

我在PHP中有一个POST,我不会总是知道我将要处理的变量字段的名称。

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>";
}

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.)

一旦我弄清楚如何获取变量名称,我还需要弄清楚如何使该函数足够智能以检测并循环数组中的变量(如果它们存在(即,如果我有一些复选框值)。

4 个解决方案

#1


41  

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

如果您只想打印整个$ _POST数组以验证数据是否正确发送,请使用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 returns true if the given variable is an array.

如果给定变量是数组,则is_array返回true。

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

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

#2


5  

You can have the foreach loop show the index along with the value:

您可以让foreach循环显示索引以及值:

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

As to the array checking, use the is_array() function:

至于数组检查,使用is_array()函数:

foreach ($_POST as $key => $entry)
{
     if (is_array($entry)) {
        foreach($entry as $value) {
           print $key . ": " . $value . "<br>";
        }
     } else {
        print $key . ": " . $entry . "<br>";
     }
}

#3


1  

It's much better to use:

使用起来要好得多:

if (${'_'.$_SERVER['REQUEST_METHOD']}) {
    $kv = array();
    foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
        $kv[] = "$key=$value";
    }
}

#4


0  

If you want to detect array fields use a code like this:

如果要检测数组字段,请使用如下代码:

foreach ($_POST as $key => $entry)
{
     if(is_array($entry)){
       print $key . ": " . implode(',',$entry) . "<br>";
     }
     else {
       print $key . ": " . $entry . "<br>";
     }
}

#1


41  

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

如果您只想打印整个$ _POST数组以验证数据是否正确发送,请使用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 returns true if the given variable is an array.

如果给定变量是数组,则is_array返回true。

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

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

#2


5  

You can have the foreach loop show the index along with the value:

您可以让foreach循环显示索引以及值:

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

As to the array checking, use the is_array() function:

至于数组检查,使用is_array()函数:

foreach ($_POST as $key => $entry)
{
     if (is_array($entry)) {
        foreach($entry as $value) {
           print $key . ": " . $value . "<br>";
        }
     } else {
        print $key . ": " . $entry . "<br>";
     }
}

#3


1  

It's much better to use:

使用起来要好得多:

if (${'_'.$_SERVER['REQUEST_METHOD']}) {
    $kv = array();
    foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
        $kv[] = "$key=$value";
    }
}

#4


0  

If you want to detect array fields use a code like this:

如果要检测数组字段,请使用如下代码:

foreach ($_POST as $key => $entry)
{
     if(is_array($entry)){
       print $key . ": " . implode(',',$entry) . "<br>";
     }
     else {
       print $key . ": " . $entry . "<br>";
     }
}