表单输入数组正在创建一个空数组!!empty()不能用于

时间:2016-05-27 18:16:13

标签: php arrays

我有一个这样的表格:

        <div class="clone-wrapper" data-id="education">
            <div class="row cloneable" data-id="0">             
                <div class="col-md-6">
                    <div class="form-group">
                        <label><?php echo System::translate("Education Institution Name"); ?></label>
                        <input placeholder="<?php echo System::translate("ex : University"); ?>" name="education[0][institution]" type="text" class="max_len" maxlength="100" value="">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label><?php echo System::translate("Education Degree"); ?></label>
                        <input placeholder="<?php echo System::translate("ex : Bachelor Degree"); ?>" name="education[0][degree]" type="text" class="max_len" maxlength="100" value="">
                    </div>
                </div>


                <div class="col-md-6">
                    <div class="form-group">
                        <label><?php echo System::translate("Date of Enrollment"); ?></label>
                        <input placeholder="<?php echo System::translate("ex : January 2001"); ?>" name="education[0][year_begin]" type="text" class="monthyearpicker" autocomplete='off' value="">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label><?php echo System::translate("Date of Completion"); ?></label>
                        <input placeholder="<?php echo System::translate("ex : December 2004"); ?>" name="education[0][year_finish]" type="text" class="monthyearpicker" autocomplete='off' value="">
                    </div>
                </div>

                <div class="col-md-12">
                    <div class="form-group">
                        <label><?php echo System::translate("Education Note"); ?></label>
                            <textarea rows="6" class="form-control" name="education[0][about]"></textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="white-space-20"></div>
        <div class="row text-right">
            <div class="col-md-12">
                <div class="btn btn-default btn-sm clonable-button" data-clone="education">
                    <i class="fa fa-plus"></i><?php echo System::translate("Add Education"); ?></div>
            </div>
        </div>

正如您所看到的,我正在使用education[0][inputname]这样的名称数组,现在我的问题是创建一个没有值的数组,所以我无法检查它们是否没有输入值到输入中。它返回:

Array
(
    [0] => Array
        (
            [company] => 
            [title] => 
            [year_begin] => 
            [year_finish] => 
            [country] => 
            [notes] => 
        )

)

如何让它返回一个空数组,以便我可以使用!empty()

1 个答案:

答案 0 :(得分:0)

只需过滤掉空元素并检查:

if(empty(array_filter($_POST['education'][0]))) {
    // array is empty
}

如果您需要检查任何空元素,请:

if(count(array_filter($_POST['education'][0])) != count($_POST['education'][0])) {
    // one or more elements are empty
}