1

How delete empty values in array in Yii2?

<?= $form->field($model, 'register_Time')->textInput() ?>
<?= $form->field($model, 'Name')->textInput() ?>

$model['register_Time'] = ''; //NULL
$model['Name'] = 'Sveta'; // Sveta

Model

class Account extends \yii\db\ActiveRecord
{

    public function rules()
    {

        return [
            [['Name', 'register_Time'], 'required']
        ];
    }

}

How delete NULL or empty values in array?

I try -

$model = array_diff($model, array(''));
3
  • php.net/manual/en/function.array-filter.php Commented Dec 29, 2015 at 13:17
  • Grumpy Why? - Error - PHP Warning – yii\base\ErrorException array_filter() expects parameter 1 to be array, object given Commented Dec 29, 2015 at 13:22
  • $model is a object of class Account. It is not an array. You cannot use in in array_filter() function/ Commented Dec 29, 2015 at 22:28

1 Answer 1

1

Simply use array_filter(), which conveniently handles all this for you:

array_filter( $model )
Sign up to request clarification or add additional context in comments.

3 Comments

Error - PHP Warning – yii\base\ErrorException array_filter() expects parameter 1 to be array, object given
Grumpy - Why? - Error - PHP Warning – yii\base\ErrorException array_filter() expects parameter 1 to be array, object given
just check dear. if you know PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.