2017-07-18 105 views
2

我有一個數組和嵌套在原始數組內的內容。數組的內容看喜歡 -消除空值嵌套數組php

$ myArray的

[0] => Array(
[ID] => 1 
[Fruit] => Apple 
[State] => Ohio 
[description] 
    Array(
    [0] => This is sample description 
    [1] => This is sample description 2 
    [2] => 
    [3] => 
    [4] => 


) 
[price] 
    Array(
    [0] => 20 
    [1] => 15 
    [2] => 
    [3] => 
    [4] => 
) 
[1] => Array(
[ID] => 1 
[Fruit] => Apple 
[State] => Ohio 
[description] 
    Array(
    [0] => This is sample description 
    [1] => This is sample description 2 
    [2] => 
    [3] => 
    [4] => 
) 
[price] 
    Array(
    [0] => 20 
    [1] => 15 
    [2] => 
    [3] => 
    [4] => 
) 

我想嵌套數組中擺脫null值。當我用下面的:

$newArray = array(); 
foreach ($firstArray as $row){ 
    if ($row !== null) 
     $newArray[] = $row; 

} 
echo $newArray; 

新數組沒有得到數組中擺脫null值。

+2

'array_filter'是你與空比較尋找 – ThisGuyHasTwoThumbs

+2

檢查'empty'值太大 –

+0

更換機器人輸出爲'var_export($ myArray)'的問題中的數組或者提供一個沙箱。 – axiac

回答

3

你可以像下面: -

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

$myArray = array_filter_to_each_sub_array_recursively($myArray); 

print_r($myArray); 

輸出: - https://eval.in/833982

+0

@ jumpman8947請檢查編輯後的答案。 –

+0

This Works,thanks for the help – jumpman8947

+0

不錯的功能!工作正常! – Cuchu