简体   繁体   English

如何检查数组中的所有变量是否都已设置

[英]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. 这段代码并没有达到预期的效果。我要实现的是,如果将$_POST所有值都设置为将它们放在$ognjen数组中,即使一个未命中,也都不应该成为$ognjen数组的一部分,但是所有这些在此foreach循环内,因为还需要执行其他一些检查。 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. 请帮忙,我觉得这很简单,但是我不知道该怎么做。

<?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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM