Use the array_filter () function in php to filter empty arrays. code _ php instance

Source: Internet
Author: User
This article mainly introduces the implementation code of using the array_filter () function to filter empty arrays in php. this is an interesting array_filter () function usage that is not intended to be found in the PHP Manual, for more information about how to use array_filter (), see the following key points: If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. if no callback function is provided, all the elements equal to FALSE will be removed. can this function be used to filter arrays with empty elements? Write an example to test the idea:

The code is as follows:


$ Entry = array (

0 => 'blog of Blue Hawaiian ',

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 );

// Output result:

Array

(

[0] => Blue Hawaiian blog

[2] => 1

Http://www.jb51.net

)


Some array elements that can be converted to Boolean FALSE are removed, and we want to get a valid array for a very comprehensive filter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.