Mofor Emmanuel
Mofor Emmanuel

Reputation: 320

how to test if all values in a two dimensional array are empty with php

i have a situation where in oder to save data from a form to the session i have to check if the $errors array which stores errors genarated by the checks done on the form data is empty.

the problem is the array is an associative array an when i use the 'empty function' to test if the array is empty it always returns flase even when all the values in the array are empty, what can i do to check if the associative array is empty, below is my code

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

Upvotes: 1

Views: 122

Answers (4)

Samir Selia
Samir Selia

Reputation: 7065

Easiest way would be to define errors array as

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

Then your condition to check if there are no errors will work perfect

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

Your code can further be optimized by removing isset from each of your conditions. Below line of code can be improved.

Original version

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

Improved version

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

Upvotes: 2

Nigel Ren
Nigel Ren

Reputation: 57131

array_filter() without a callback removes the empty elements. So you could do something like...

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

Upvotes: 0

Abzkn
Abzkn

Reputation: 51

you can define

$errors = array();

instead of

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

and then do you validation and can check with empty($errors) or count($errors)

Upvotes: 0

Girish Patil
Girish Patil

Reputation: 154

Because you have initialized the $errors with its keys, it will return false when checked if empty. Instead the best way to do this would be.

$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
}

Upvotes: 1

Related Questions