dqa35710 2014-10-23 12:19 采纳率: 0%
浏览 48
已采纳

循环通过$ _POST。 循环结束后如何停止?

I want this function to return false when for each statement has stopped looping. And true if it's not empty.

function check_empty($post_array){
        $items_in_array = count($post_array); //array length
        echo $items_in_array;
        foreach($post_array as $key=>$value){
            if(empty($value)){
                echo $key . " field cannot be left empty" . "</br>";
            }
        }
        return false;
    }

Note: if i return false inside foreach it will stop right on first iteration i want it to complete iteration and only stop when it has iterated over all the values in that array($post_array)

  • 写回答

6条回答 默认 最新

  • duanjing3656 2014-10-23 12:35
    关注

    if i return false inside foreach it will stop right on first iteration i want it to complete iteration and only stop when it has iterated over all the values

    So an empty value would be considered an “error” of some sort, and one such error should be enough to make the function return false?

    function check_empty($post_array){
        $no_empty_fields = true;
        foreach($post_array as $key=>$value){
            if(empty($value)){
                $no_empty_fields = false;
                // plus whatever additional handling you need here
            }
        }
        return $no_empty_fields;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)