开发者社区> 问答> 正文

检查所有数组项是否为空PHP

我正在从表单中添加一系列项目,如果所有项目都是空的,我想执行一些验证并将其添加到错误字符串中。所以我有:

$array = array( 'RequestID' => $_POST["RequestID"], 'ClientName' => $_POST["ClientName"], 'Username' => $_POST["Username"], 'RequestAssignee' => $_POST["RequestAssignee"], 'Status' => $_POST["Status"], 'Priority' => $_POST["Priority"] ); 然后,如果所有数组元素均为空,请执行:

$error_str .= '

  • Please enter a value into at least one of the fields regarding the request you are searching for.
  • ';

    问题来源于stack overflow

    展开
    收起
    保持可爱mmm 2020-02-08 20:14:06 387 0
    1 条回答
    写回答
    取消 提交回答
    • 您可以只使用内置的array_filter

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

      因此可以在一条简单的代码行中做到这一点。

      if(!array_filter($array)) { echo '

    • Please enter a value into at least one of the fields regarding the request you are searching for.
    • '; }

    2020-02-08 20:14:19
    赞同 展开评论 打赏
    问答分类:
    PHP
    问答标签:
    问答地址:
    相关问答
    607
    1
    0
    1070
    1
    0
    499
    1
    0
    551
    1
    0
    715
    1
    0
    526
    1
    0
    657
    1
    0
    497
    1
    0
    443
    1
    0
    678
    1
    0
    问答排行榜
    最热
    最新

    相关课程

    更多
    推荐问答

    相关文章

    相关电子书

    更多
    PHP安全开发:从白帽角度做安全 立即下载
    PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
    复杂PHP系统性能瓶颈排查及优化 立即下载

    相关实验场景

    更多