1

after submited form i want to check array if array is empty alert error for user. but i get error when submited form:

PHP

    $errors = array_filter($_POST['session']);
    if (!empty($errors)) {
        foreach ($_POST['session'] as $value) {
            $session.=$value.',';
        }
        $session=substr($session, 0 , -1);
    }

Warning: array_filter() expects parameter 1 to be array, null given in C:\inetpub\wwwroot\manage\test_bank\index.php on line 729

7 Answers 7

3

You need to check wheather it is an array or not, before doing any array operation.

if(is_array($_POST['session'])){
  $errors = array_filter($_POST['session']);
} 
Sign up to request clarification or add additional context in comments.

Comments

1

The warning occurs because array_filters() requires an array to be passed to it. Before passing $_POST['session'] to this function, very if that it is an array:

if(is_array($_POST['session'])) {
    $errors = array_filter($_POST['session']);
    // continue on
}

Comments

0

use is_arrayfor checking weather it is array or not.

echo is_array($_POST['session']);

Comments

0

This is because $_POST is not an array I guess you are looking for this :

$errors = array_filter($_POST);

Comments

0

The following would most simply check for empty errors or not

!empty($_POST['session'])

Would work provided you are not stuffing empty entries in the $_POST['session'] in no error cases. Why do you need the array_filter?

Comments

0

$_POST is an array, but here $_POST['session'] is not.

You can smply try this:

if(isset($_POST['session']))
{
    //do your stuff
}

Comments

0

Change it to array_filter($_POST) because $_POST is an assoc array, or check if $_POST['session'] is an array using this line is_array($_POST['session']) before the array_filter(). You should check first if the variable you are working with is an array before using array functions.

Comments