如何遍历$ _POST传递每个帖子字段作为参数传递给函数? [英] How to loop through $_POST passing each post field as a parameter to a function?Edit

查看:88
本文介绍了如何遍历$ _POST传递每个帖子字段作为参数传递给函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在算法上表示我想使用PHP中的foreach循环和$ _POST数组实现的目标.

I am just representing algorithmically what i want to achieve using a foreach loop and the $_POST array in php.

    function check_fields($_POST)
{
 // provide $_post['field1'],$_post['field2']..so on

  foreach($_POST as $_POST['FIELD 1-N'] 
{

 isvalid($_POST['FIELD1 - N']);
}
}

希望您能理解我要问的问题...

Hope you understand what I'm trying to ask...

$ _POST ['FIELD1'],$ _ POST ['FIELD1']等是传递给isvalid()函数的变量名称. isvalid()函数的作用是使用isset()并清空以检查变量是否包含值...

$_POST['FIELD1'], $_POST['FIELD1'] and so on are variable names that are passed to isvalid() function. What The isvalid() function does is use isset() and empty to check if the variable contains a value...

推荐答案

使用foreach,您不必知道数组中键的名称.

With foreach, you don't have to know the names of the keys in the array.

foreach($_POST as $key => $field) {
    isvalid($field);
}

$key包含诸如"field1","field2"之类的名称,而$field包含$_POST数组内部的值.然后,foreach循环将在每个字段值上运行函数invalid().

$key contains the names like "field1", "field2" and so on while $field contains the value inside the $_POST array. The foreach loop will then run the function invalid() on each of the field value.

要检查是否设置了字段值:

To check if the field values are set:

// Sample $_POST array
$_POST = array(
    "field1" => "", // this is not set
    "field2" => "data"
);
foreach($_POST as $key => $field) {
    // You can check if it is empty using foreach alone
    if (strlen($field) > 0) {
        // this field is set
    } else {
        // this field is not set
    }
}

您也可以使用empty(),但是它将"0"视为空,因此请小心.

You can use empty() as well but it treats "0" as empty so be careful.

这篇关于如何遍历$ _POST传递每个帖子字段作为参数传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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