使用PHP数组验证表单

时间:2014-11-16 09:37:19

标签: php arrays

我有这个代码,我希望有一个包含必填字段的数组,并且只循环遍历列表中的字段。

                    //Required Fields   
                $validationFields = array(
                    "username" => "",
                    "email" => "",
                    "password" => ""
                    );


                foreach ($_POST as $key => $value)
                {   
                    $asd = in_array($_POST[$key], $validationFields);

                    echo "<pre>";
                    var_dump($asd);
                    echo "</pre>";

                    if(in_array($_POST[$key], $validationFields))
                    {
                        if($_POST[$key] == "")
                        {
                            $error = "has-error";
                        }
                    }

                }

1 个答案:

答案 0 :(得分:0)

尝试查看array_intersect或您需要array_intersect_keys

http://php.net/manual/en/function.array-intersect-key.php