【问题标题】:how to test if all values in a two dimensional array are empty with php如何使用php测试二维数组中的所有值是否为空
【发布时间】:2018-01-14 07:58:08
【问题描述】:

我有一种情况,为了将数据从表单保存到会话中,我必须检查 $errors 数组是否存储了对表单数据进行的检查生成的错误。

问题是数组是一个关联数组,当我使用“空函数”来测试数组是否为空时,即使数组中的所有值都为空,它也总是返回 flase,我该怎么做才能检查是否关联数组为空,下面是我的代码

session_start();

// define errors array 
$errors = array(
'name' => "",
'username' => "",
'age' => "");

// check and sanitize form data
if (isset($_POST["name"]) and empty($_POST["name"])) {
     $errors['name'] = "your name is needed";
     }else{
         $_POST['name'] = test_input($_POST['name']);
         if(!preg_match("/^[a-zA-Zéèàêâùïüë.',_  ]+$/",$_POST['name'])) {
             $errors['name'] = "only letters and white spaces accepted";
             }else{
                $validator++; 
             }
     }

    if (isset($_POST["username"]) and empty($_POST["username"])) {
        $errors['username'] = "your username is needed";
     }else{
         $_POST['username'] = test_input($_POST['username']);
         if(!preg_match("/^[a-zA-Zéèàêâùïüë.',_  ]+$/",$_POST['username'])) {
             $errors['username'] = "only letters and white spaces accepted";
             }else{
                $validator++; 
             }
     }

    if (isset($_POST["age"]) and empty($_POST["age"])) {
         $errors['age'] = "age is needed";
     }else{
         $_POST['age'] = test_input($_POST['age']);
         if(!preg_match("/^[1-90]+$/",$_POST['age'])) {
           $errors['age'] = "only numbers accepted";
             }else{
                $validator++; 
             }
     }
// check if data is correct and save to session
if(empty($errors)){
    // save data in session 
    $_SESSION['name'] = $_POST['name'];
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['age'] = $_POST["age"];
}else{
    session_destroy();
    header("location :index.php");
}`

【问题讨论】:

标签: php arrays


【解决方案1】:

因为你已经用它的键初始化了$errors,如果它为空,它会在检查时返回false。相反,最好的方法是。

$errors = array();
// if error occurs then $errors["name"] = "Name error";
// Now you can check if $errors is empty or not
if(empty($errors)){
  // store it to session
}

【讨论】:

  • @MoforEmmanuel 随时!如果有帮助,请接受这个作为答案
【解决方案2】:

最简单的方法是将错误数组定义为

// define errors array 
$errors = array();

那么您检查是否没有错误的条件将完美运行

// check if data is correct and save to session
if(empty($errors)){
      // Logic 
}

可以通过从每个条件中删除 isset 来进一步优化您的代码。下面的代码行可以改进。

原版

if (isset($_POST["name"]) and empty($_POST["name"])) {
    // Logic
}

改进版

if (empty($_POST["name"])) {
    // Logic 
}

【讨论】:

    【解决方案3】:

    你可以定义

    $errors = array();
    

    而不是

    $errors = array(
    'name' => "",
    'username' => "",
    'age' => "");
    

    然后您进行验证并可以通过empty($errors)count($errors) 进行检查

    【讨论】:

      【解决方案4】:

      array_filter() 没有回调会删除空元素。所以你可以做类似...

      if( empty( array_filter($errors))){
          // save data in session 
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-26
        • 1970-01-01
        相关资源
        最近更新 更多