检查所有数组项是否为空 PHP [英] Checking if all the array items are empty PHP

查看:51
本文介绍了检查所有数组项是否为空 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从表单中添加一组项目,如果所有项目都为空,我想执行一些验证并添加到错误字符串中.所以我有:

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>';

推荐答案

你可以使用内置的 array_filter

如果未提供回调,则所有等于 FALSE 的输入条目(请参阅转换为布尔值)将被删除.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆