2012-09-20 22 views
0

可能重複:
how to count non-empty entries in php array?計數數組中非空項目的數量?

我希望計算數組不爲空/空鍵的數量。

EG: 
array(1>'asdf',2>'fdas',3>''); 

count($array) would return 3, I want a function that returns 2 as 3 is empty 

是否有任何PHP內置函數來做到這一點?如何做呢?

+0

http://stackoverflow.com/questions/9789083/exclude-null-values-from-count-in-array http://stackoverflow.com/questions/4422889/how-to-count-non -phpty-entries-in-php-array – RezaSh

+0

學習[PHP的數組函數](http://php.net/manual/en/ref.array.php)和[成爲更好的PHP開發人員](http:// jason .pureconcepts.net/2012/08 /更好的PHP開發者/) –

回答

2

使用:

count(array_filter($array)); 

陣列array_filter(數組$輸入[,可調用$回調= 「」])

如果沒有回調被提供時,等於FALSE輸入的所有條目(見 轉換爲布爾值)將被刪除。