How to remove empty values from multidimensional array in PHP?

12,546

Solution 1

Bit late, but may help someone looking for same answer. I used this very simple approach to;

  1. remove all the keys from nested arrays that contain no value, then
  2. remove all the empty nested arrays.

$postArr = array_map('array_filter', $postArr);
$postArr = array_filter( $postArr );

Solution 2

The following function worked for my case. We can use a simple recursive function to remove all empty elements from the multidimensional PHP array:

function array_filter_recursive($input){
    foreach ($input as &$value){
        if (is_array($value)){
            $value = array_filter_recursive($value);
        }
    }
    return array_filter($input);
}

Then we just need to call this function:

$myArray = array_filter_recursive($myArray);

Solution 3

Not sure if this is exactly what your looking for.

function array_remove_null($array) {
    foreach ($array as $key => $value)
    {
        if(is_null($value))
            unset($array[$key]);
        if(is_array($value))
            $array[$key] = array_remove_null($value);
    }
    return $array;
}

Update (corrections):

function array_remove_null($array) {
    foreach ($array as $key => $value)
    {
        if(is_null($value))
            unset($array[$key]);
        if(is_string($value) && empty($value))
            unset($array[$key]);
        if(is_array($value))
            $array[$key] = array_remove_null($value);
        if(isset($array[$key]) && count($array[$key])==0)
            unset($array[$key]);
    }
    return $array;
}

I'm sure better checking and making this more robust might help the solution.

Share:
12,546
Richard-MX
Author by

Richard-MX

Updated on June 05, 2022

Comments

  • Richard-MX
    Richard-MX about 2 years

    I've been looking a lot of answers, but none of them are working for me.

    This is the data assigned to my $quantities array:

    Array(
        [10] => Array([25.00] => 1)
        [9] => Array([30.00] => 3)
        [8] => Array([30.00] => 4)
        [12] => Array([35.00] => )
        [1] => Array([30.00] => )
        [2] => Array([30.00] => )
    )
    

    I'm looking for a way to remove the subarrays with empty values like [12] [1] and [2] while keeping everything else.

    The desired result:

    Array(
        [10] => Array([25.00] => 1)
        [9] => Array([30.00] => 3)
        [8] => Array([30.00] => 4)
    )
    

    I tried a lot of the functions on the official php docs and none of them worked.

    I've used this one:

    function array_filter_recursive($array, $callback = null) {
        foreach ($array as $key => & $value) {
            if (is_array($value)) {
                $value = array_filter_recursive($value, $callback);
            } else {
                if ( ! is_null($callback)) {
                    if ( ! $callback($value)) {
                        unset($array[$key]);
                    }
                } else {
                    if ( ! (bool) $value) {
                        unset($array[$key]);
                    }
                }
            }
        }
        unset($value);
        return $array;
    }
    

    But it only removes the element in the subarrays; I need the subarrays to be removed entirely.

    I don't want this:

    Array(
        [10] => Array([25.00] => 1)
        [9] => Array([30.00] => 3)
        [8] => Array([30.00] => 4)
        [12] => Array()
        [1] => Array()
        [2] => Array()
    )
    
  • Richard-MX
    Richard-MX about 12 years
    Hello Jason, thanks for your reply. Unfortunately, it doesn't work for what i need. As Paul answer, it returns the same as the original question.
  • mickmackusa
    mickmackusa over 6 years
    This is an exact duplicate method (character for character) of NaitikShah's answer that was posted two months earlier. If you cannot improve your answer to provide better information you should delete this answer because it does not add value to the page (it is merely page bloat).
  • mickmackusa
    mickmackusa over 6 years
    This answer DOESN'T WORK. It doesn't work in the posted demo link. It doesn't work in my demo link How in the world did this earn 3 upvotes? Downvote from me.
  • mickmackusa
    mickmackusa over 6 years
    This answer DOESN'T WORK. It doesn't work in my demo link How in the world did this earn 3 upvotes? Downvote from me.
  • mickmackusa
    mickmackusa over 6 years
    I'm not sure why this has 4 upvotes. This code-only answer does not work. Demo
  • Jason Foglia
    Jason Foglia over 6 years
    @mickmackusa this example worked back in the days of PHP 5.
  • mickmackusa
    mickmackusa over 6 years
    @JasonFoglia There is nothing version specific that is impacting your answer. It doesn't work in php5 either. PHP 5.5.0.a.2 Demo
  • Jason Foglia
    Jason Foglia over 6 years
    @mickmackusa This answer answers the question. How to remove VALUES from an array; not empty arrays from arrays.
  • Jason Foglia
    Jason Foglia over 6 years