How to remove empty array from multidimensional array?

3k views Asked by At

Here is the multidimension array, and I want to remove the empty array in it.

$exclude1=array(

array(

    [0] => Last Updated: Thu Apr 21 17:03:23 2016 PST

)
Array

(

    [0] => 

)

Array

(

[0]=>

)

Array

(

    [0] => MODEL: MODEL_NC

)

)

I want to remove empty arrays. I have tried this:

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

but it is not working.

4

There are 4 answers

4
AudioBubble On

use array_filter like this way:

$exclude1=array_filter( $exclude1, function($v){return count($v) == 0 ? false : true;});
3
Rwd On

To remove empty arrays from an array you should be able to just do:

$newArray = array_filter($exclude1);

Hope this helps!

7
Md. Abutaleb On

Your current array values are following:

$exclude1 = array(
    array("Last Updated: Thu Apr 21 17:03:23 2016 PST"),
    array(""),
    array(""),
    array("MODEL: MODEL_NC"),
);

See the output of current array. it's look like the follwoing :

echo "<pre>";
print_r($exclude1);

Output will be:

Array
(
    [0] => Array
        (
            [0] => Last Updated: Thu Apr 21 17:03:23 2016 PST
        )

    [1] => Array
        (
            [0] => 
        )

    [2] => Array
        (
            [0] => 
        )

    [3] => Array
        (
            [0] => MODEL: MODEL_NC
        )

)

Use array_filter and array_map

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

Check the final output:

Array
(
    [0] => Array
        (
            [0] => Last Updated: Thu Apr 21 17:03:23 2016 PST
        )

    [3] => Array
        (
            [0] => MODEL: MODEL_NC
)

So it will remove empty array from your previous multidimensional array.

2nd Way

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;
}

3rd Way:

$arr = array_filter_recursive($arr);
$arr = array_filter($arr);

Reference: http://php.net/manual/en/function.array-filter.php

0
LF-DevJourney On

In you code, you wrongly used the array_filter, you can check its menual.

array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )

So you can use this way.

<?php
$exclude1=array(
    ['Last Updated: Thu Apr 21 17:03:23 2016 PST'],
    [null],
    [null],
    ['MODEL: MODEL_NC']
);
unset($exclude1[1][0]);  //this make the array empty as you question.
unset($exclude1[2][0]);
var_dump($exclude1);
$exclude1=array_filter( $exclude1, function($v){return count($v) == 0 ? false : true;});
var_dump($exclude1);

and the output:

ei@localhost:~$ php test.php
array(4) {
  [0]=>
  array(1) {
    [0]=>
    string(42) "Last Updated: Thu Apr 21 17:03:23 2016 PST"
  }
  [1]=>
  array(0) {
  }
  [2]=>
  array(0) {
  }
  [3]=>
  array(1) {
    [0]=>
    string(15) "MODEL: MODEL_NC"
  }
}
array(2) {
  [0]=>
  array(1) {
    [0]=>
    string(42) "Last Updated: Thu Apr 21 17:03:23 2016 PST"
  }
  [3]=>
  array(1) {
    [0]=>
    string(15) "MODEL: MODEL_NC"
  }
}