0

I have array structure like this,

Array
(
    [1] => Array
        (
            [1] => 
            [2] => 
            [3] => 
            [4] => Product
            [5] => Product Name
            ..
            [59] => Color
        )

    [2] => Array
        (
            [1] => 
            [2] => 
            [3] => 1
            [4] => 9155
            ....
            [59] => 
        )

    [3] => Array
        (
            [1] => 
            [2] => 
            [3] => 1
            [4] => 9165
            ...
            [59] => Green
        )

    [4] => Array
        (
            [1] => 
            [2] => 
            [3] => 
            [4] => 
           ...
            [58] => 
            [59] => 
        )
        )

Its reading data from Excel sheets , the issue is when i read data from excel sheet it reads empty rows too, I already tried to ignore empty rows from the excel sheet some how its working (when the excel is created from MSexcel ) but from Google Drive its reading empty rows. So I would like to remove those rows with 1- 59 are blanks. in the above example array with index 4 .

Note that some index have missing values in many sub index but I don't want to remove those, only all sub indexes from 1-59 are blank then that main index (here its 4) needs to remove.

Is there any smart way to remove those array index that have empty values. I not like to iterate all the array and store to another.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Jobin
  • 8,238
  • 1
  • 33
  • 52
  • possible duplicate. http://stackoverflow.com/questions/9895130/php-remove-empty-array-elements-from-a-multidimensional-array – Panama Jack Jan 23 '14 at 07:37

4 Answers4

2

Use array_map

$array = array_map('array_filter', $array);
Linga
  • 10,379
  • 10
  • 52
  • 104
2

if you want to remove the index 4 that is an empty array :

array_filter(array_map('array_filter', $array)); 
Andreea
  • 139
  • 12
1

let try with array_filter

$entry = array(
             0 => 'foo',
             1 => false,
             2 => -1,
             3 => null,
             4 => ''
          );

print_r(array_filter($entry));

Array
(
    [0] => foo
    [2] => -1
)
zerokavn
  • 1,333
  • 1
  • 9
  • 10
0

Use array_filter..It will remove all empty values..

array_filter($array);
Vikas Arora
  • 1,666
  • 2
  • 17
  • 38