2015-04-06 137 views
0
Array (
     [0] => Angiogram - $10,000  
     [1] => 
     [2] => 
     [3] => 
    ) 

這是我的數組格式,我需要從該數組中刪除所有空索引。我試過使用array_filter(),但它不工作。請幫忙。 我需要像結果:如何從數組中刪除空值?

Array([0]=> Angiogram-$10,000) 
+4

'$陣列= array_filter($陣列);'工作得很好 – 2015-04-06 11:22:15

+0

array_filter不工作,感謝您的答覆 – ManoharSingh 2015-04-06 11:24:03

+2

那麼你沒有空元素!什麼是輸出:'var_dump($ arr);'? – Rizier123 2015-04-06 11:24:37

回答

2

試試這個..

$array=array("Angiogram - $10,000","","","",""); 
$removeempty=array_filter($array); 
print_r($removeempty); 

    or 

$array = array_filter(array_map('trim', $array)); 
print_r($array); 

    Ans: 

    Array ([0] => Angiogram - $10,000) 
+0

謝謝非常感謝@Jocker – ManoharSingh 2015-04-06 11:35:02

2

請嘗試這樣,

array_filter(array_map('trim', $array)) 
+0

它工作嗎? – 2015-04-06 11:29:36

+0

是@Manadh謝謝非常工作 – ManoharSingh 2015-04-06 11:34:41

+1

進一步簡化爲'$ myArray = array_filter($ myArray,'trim');'並且不需要完全調用'array_map()' – 2015-04-06 11:35:23

0

如果array_filter($陣列)不工作就意味着你數組不是空的!

試試這個:

$array = array("1", "2", "3", "","5"); 


$clearArray = var_dump(removeEmpty($array)); 

function removeEmpty($array) { 
    return array_filter($array, 'removeEmpty_internal'); 
} 

function removeEmpty_internal($value) { 
    return !empty($value) || $value === 0; 
}