php中使用array_filter()函數過濾空數組的實現代碼_php執行個體

來源:互聯網
上載者:User

今天在翻看手冊查詢array_filter()的使用細節時,看到了個小關鍵點:If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. 如果沒有給出回呼函數,所有的等於 FALSE 的元素將會被移除掉,這不正好可以用來過濾一些有空元素的數組嗎?連忙寫個例子測試一下想法:

複製代碼 代碼如下:

$entry = array( 

             0 => '藍色夏威夷的部落格', 

             1 => false, 

             2 => 1, 

             3 => null, 

             4 => '', 

             5 => 'http://www.jb51.net', 

             6 => '0',

             7 => array(),

             8 => 0

          );

$validarr = array_filter($entry);

print_r($validarr);

//輸出結果:

Array

(

    [0] => 藍色夏威夷的部落格

    [2] => 1

    [5] => http://www.jb51.net

)


一些可以轉化為布爾型FALSE的數組元素都被移除了,對我們想要得到一個有效數組作了一個非常全面的過濾。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.