sergeevpasha/laravel-dpd

View on GitHub
src/Helpers/DPDHelper.php

Summary

Maintainability
A
25 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace SergeevPasha\DPD\Helpers;

class DPDHelper
{
    /**
     * Remove null values from an array recursively
     *
     * @param array $haystack
     *
     * @return array
     */
    public static function removeNullValues(array $haystack): array
    {
        foreach ($haystack as $key => $value) {
            if (is_array($value)) {
                $haystack[$key] = self::removeNullValues($haystack[$key]);
            }
            if (empty($haystack[$key]) && $haystack[$key] !== false) {
                unset($haystack[$key]);
            }
        }
        return $haystack;
    }
}