简体   繁体   English

如果为空则删除数组

[英]Remove array if empty

I have an array of errors for file upload. 我在上传文件时遇到了一系列错误。 It display errors message if the upload went wrong. 如果上传错误,它将显示错误消息。 But if the array is getting no errors, it display "Array" on my view. 但是,如果数组没有错误,则在我的视图中显示“ Array”。

How to remove the "Array" if it's empty? 如果“数组”为空,如何删除?

My controller: 我的控制器:

if (!is_null($avatar) && !empty($avatar)) {
            if ($_FILES && $_FILES['avatar']['name'] !== "") {
                if (!$this->upload->do_upload('avatar')) {
                    $data['errors'] = array('error' => $this->upload->display_errors());
                } else {
                    $image = $this->upload->data();
                    $avatar = $image['file_name'];
                }
            }
        }
$this->load->view('auth/edit_profile_form', $data);

My view: 我的观点:

<div class="errors">
<?php echo $errors; ?>
</div>

检查错误数组上的条目数量:

echo (count($errors) > 0 ? implode("<br>", $errors) : '');

In your view you want to do 在您看来,您想做

<?php if(is_array($errors) && !empty($errors)) { ?>
    <div class="errors">
        <?php foreach($errors as $error) { ?>
            <?php echo $error; ?>
        <?php } ?>
    </div>
<?php } ?>

This checks to see if there is anything in the array and the loop through each item in the array and echo's it 这将检查数组中是否有任何内容,并遍历数组中的每个项目并回显它

This checks if the $errors variable is set. 这将检查$errors变量是否已设置。 If $errors['error'] it contains something, the <div class="errors"> will be printed. 如果$errors['error']包含某些内容,则会显示<div class="errors">

<?php
if ($errors['error']) {
     echo '<div class="errors">'.$errors['error'].'</div>';
}
?>

In your view: 您认为:

<?php if ($errors['error']): >
<div class="errors">
  <?php echo $errors['error']; ?>
</div>
<?php endif; >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM