Avatar ·

How to remove empty elements from an array in PHP?

Hello everyone! I'm a beginner PHP developer and I have a problem with arrays. I'm processing data from a form where users can leave fields empty. As a result, I get an array that contains empty strings, null values, and even false. I need to clean this array from all "empty" elements so that only real data remains.

Here is an example of my code:

<?php
$data = [
    'name' => 'Ivan',
    'email' => '',
    'phone' => '123-456',
    'comment' => null,
    'age' => 0,
    'active' => false,
    'extra' => '   ',
    'tags' => []
];
?>

I tried using array_filter(), but it doesn't remove everything I need. For example, 0 and false are also considered