dqa35710 2014-10-23 04: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 04: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部