Ognj3n
Ognj3n

Reputation: 759

How to check if all variables in array are set

First of all, I know this question already exists and already has answers, but I would like to stress in which way my question is different. I have code like this:

$ognjen=array();
    foreach($_POST as $key=>$value){
        if($key!='prenos'&& $key!='submit'){
            if (strrpos($key, '1', -1)){
                if(!empty($_POST[$key])){
                    $uslov=true;//kontrolna promjenljiva
                }else{
                    $uslov=false;
                }
                if($uslov==true){
                    $ognjen[]=$value;
                }else{
                    $_SESSION['message']='You must fill out all fields';
                    unset($_SESSION['message']);
                }
            }//ovdje ide elseif
        }
    }

This code doesn't do what's expected.What I want to achieve is if all values of $_POST are set to put them in $ognjen array,if even one misses, none of them should become part of $ognjen array, but all that inside this foreach loop, because there are some other checkings that need to fulfill. And while searching for answers I couldn't find any that fits to my situation. Please help, I feel that this is pretty simple task to do, but I don't know way to do it.

Upvotes: 1

Views: 245

Answers (1)

Paresh Gami
Paresh Gami

Reputation: 4782

<?php
function is_blank($data)
{
    $error_message="";
    foreach ($data as $key => $value)
    {
        if($value=="" or $value==null)
        {
            $error_message.=$key.",";
        }
    }
    if($error_message!="")
    {
        $error_message=substr($error_message, 0,-1);
        return $error_message." is cannot be null or empty.";
    }
    else
    {
        return "";
    }
}

echo is_blank($_POST);

Upvotes: 1

Related Questions