Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

I have this array under $quantities variable. If i print_r, i got this:

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] => 
        )
)

But, im looking for a way to remove those with empty values like [12] [1] and [2] and keep everything else.

Array
(
    [10] => Array
        (
            [25.00] => 1
        )

    [9] => Array
        (
            [30.00] => 3
        )

    [8] => Array
        (
            [30.00] => 4
        )

)

Thanks. Sorry if this looks kinda simple, im a newbie and i dont know how to do it. 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 i get this:

Array ( [10] => Array ( [25.00] => 1 )

[9] => Array
    (
        [30.00] => 3
    )

[8] => Array
    (
        [30.00] => 4
    )

[12] => Array
    (
    )

[1] => Array
    (
    )

[2] => Array
    (
    )

)

Is almost what i want, but, i would like to remove [12] [1] and [2] arrays.

share|improve this question
    
Consider using var_dump() instead to show us the array contents. – Ja͢ck May 14 '13 at 11:57

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 );
share|improve this answer
    
Worked for me !!!! Thanks!!!! – TharinduLucky Dec 1 '15 at 14:41

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;
}
share|improve this answer
    
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. – Richard-MX Apr 18 '12 at 17:44
1  
Thank you this helped me – Mansoorkhan Cherupuzha Dec 10 '14 at 5:42

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);
share|improve this answer

use array_map() for filter every array in $array:

$newArray = array_map('array_filter', $array);

Here's demo

share|improve this answer
$newArray = array_map('array_filter', $array);

This sintax it's work and very help full thank's guys ..

share|improve this answer

Just call array_filter one more time. That will filter out all the empty values from your result:

// $arr contains your multi dimensional array

$arr = array_filter_recursive($arr);
$arr = array_filter($arr);
share|improve this answer
    
Not working my friend. It returns the same result. – Richard-MX Apr 18 '12 at 17:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.