检查是否所有的数组项是空的PHP [英] Checking if all the array items are empty PHP
本文介绍了检查是否所有的数组项是空的PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是从一种形式添加项目的数组,如果所有的人都是空的,我想执行一些验证,并加入到一个错误字符串。所以,我有:
$阵列=阵列(
请求ID => $ _ POST [请求ID],
CLIENTNAME'=> $ _ POST [CLIENTNAME],
用户名= GT; $ _ POST [用户名],
RequestAssignee'=> $ _ POST [RequestAssignee],
状态=> $ _ POST [状态],
'优先'=> $ _ POST [优先级]
);
然后,如果所有的数组元素都是空的执行:
$ ERROR_STR ='<李方式>请输入一个值到字段中的至少一个关于您正在搜索的请求和LT; /李>'。
解决方案
您可以只使用内置的 array_filter
如果没有回调提供,等于FALSE(见转换为布尔)输入的所有条目都将被删除。
块引用>所以,可以在一个简单的线条做到这一点。
如果(!array_filter($阵列)){
呼应'<李方式>请输入一个值到字段中的至少一个关于您正在搜索的请求和LT; /李>';
}I'm adding an array of items from a form and if all of them are empty, I want to perform some validation and add to an error string. So I have:
$array = array( 'RequestID' => '$_POST["RequestID"]', 'ClientName' => '$_POST["ClientName"]', 'Username' => '$_POST["Username"]', 'RequestAssignee' => '$_POST["RequestAssignee"]', 'Status' => '$_POST["Status"]', 'Priority' => '$_POST["Priority"]' );
And then if all of the array elements are empty perform:
$error_str .= '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>';
解决方案You can just use the built in array_filter
If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.
So can do this in one simple line.
if(!array_filter($array)) { echo '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>'; }
这篇关于检查是否所有的数组项是空的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文