2013-07-04 89 views
0

我的多維數組是這樣的:無法從多維數組PHP刪除空值

Array 
(
[0] => Array 
    (
     [0] => 2010-12-03 
     [1] => 0 
     [2] => Array 
      (
       [0] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [1] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [2] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [3] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [4] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [5] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

      ) 

    ) 

[1] => Array 
    (
     [0] => 2010-12-10 
     [1] => 486 
     [2] => Array 
      (
       [0] => Array 
        (
         [0] => Bob 
         [1] => Lucy 
         [2] => 54 
         [3] => Y 
         [4] => PC1Clean 
        ) 

       [1] => Array 
        (
         [0] => Jo 
         [1] => Mary 
         [2] => 432 
         [3] => Y 
         [4] => PC2Bar 
        ) 

       [2] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [3] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [4] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

       [5] => Array 
        (
         [0] => 
         [1] => 
         [2] => 0 
         [3] => 
         [4] => 
        ) 

      ) 

    ) 

我試過array_filter和不同的循環迭代刪除zero/null值,如

function removeElementWithValue($array, $key, $value) { 
    foreach($array as $subKey => $subArray) { 
     if($subArray[$key] == $value) { 
      unset($array[$subKey]); 
     } 
    } 
} 

但似乎沒有任何工作。任何幫助將非常感激!

+0

我認爲這是變量'$ array'範圍的問題。 – fehnomenal

+2

您需要將參考傳遞給您的函數,否則您不會從原始數組中取消設置 –

+0

http://rogerpadilla.wordpress.com/2009/08/21/remove-empty-items-from-array/ – softsdev

回答

3

您需要遞歸調用您的remove函數。 (爲數組中的每個子值調用該函數,然後該函數將自動爲每個子子值等自動調用它,直到它無法再向下搜索樹)。

這裏是一個應該刪除所有空值(包括數組,如果他們是空的)一個迅速作出(未經測試)功能:

function removeEmptyElements($array) { 
    foreach ($array as $key => $value){ 
     if (empty($value)) { 
      unset($array[$key]); 
     } else if (is_array($value)) { 
      $array[$key] = removeEmptyElements($value); 
     } 
    } 
    return $array; 
} 
+0

是的,但數組也應該通過引用傳遞。否則,更改只會影響本地副本。 –

+0

@GeorgeCummins考慮到函數具有'return $ array',我認爲這很明顯,你可以使用'$ array = removeEmptyElements($ array);''來調用它。 – h2ooooooo

+0

啊,你說得對。這與OP調用函數的原始方式有所不同,因此可能值得注意。 –

0

也許這個答案是有點太晚了,但在這裏它是:

$array = array(// array with examples 
    array('123',' 456', array(123, null)), 
    array('', null, '123'), 
    123, 
    234, 
    0, 
    null, 
); 

$filterEmptyValues = function($val) use (&$filterEmptyValues) { 
    foreach($val as $key => $value) { 
     if (!is_array($value)) { 
      if (!$value) unset($val[$key]); 
     } 
     else $val[$key] = call_user_func($filterEmptyValues, $value); 
    } 
    return $val; 
}; 


$array = $filterEmptyValues($array) ; 

print_r($array);