开发者社区 问答 正文

检查所有数组项是否为空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 425 分享 版权
    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
    问答标签:
    问答地址:
    相关问答
    758
    2
    0
    733
    1
    0
    1172
    1
    0
    618
    1
    0
    616
    1
    0
    805
    1
    0
    599
    1
    0
    717
    1
    0
    546
    1
    0
    502
    1
    0
    问答排行榜
    最热
    最新

    相关文章

    还有其他疑问?
    咨询AI助理