How to Remove Empty Array Elements in PHP

Here’s a handy code snippet which allows you to remove empty elements from an array in PHP.

<?php
function ccw_remove_empty_element_from_array($data)
{
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $data[$key] = ccw_remove_empty_element_from_array($data[$key]);
        }

        if (empty($data[$key])) {
            unset($data[$key]);
        }
    }

    return $data;
}

Read Also : Login with Facebook in Laravel 5 Application

0 Comments

Leave a Comment.