Let's talk about the array_filter () function.

Source: Internet
Author: User
During the development process, when an array is null, it is wrong to directly judge whether empty is null, because when this value is a multi-dimensional number, empty results have a value, use array

During the development process, when an array is null, it is wrong to directly judge whether empty is null, because when this value is a multi-dimensional number, empty results have a value, use the array_filter function to easily remove multi-dimensional null values. the subscript of the array has not changed. paste the following online usage:
 

PHP code
  1. $ Entry = array (
  2. 0 => '9th Street php ',
  3. 1 => false,
  4. 2 => 1,
  5. 3 => null,
  6. 4 => '',
  7. 5 => 'http: // www.9streets.cn ',
  8. 6 => '0'
  9. );
  10. Print_r (array_filter ($ entry ));
  11. ?>
  12. The output result of the above code is:
  13. Array
  14. (
  15. [0] => Ninth Street PHP
  16. [2] => 1
  17. Http://www.9streets.cn
  18. )

Let's see how array_filter is defined:

The array_filter () function uses the callback function to filter elements in the array. if the custom filter function returns true, the current value of the operated array is included in the returned result array, and form a new array of results. If the original array is an associated array, the key name remains unchanged.
 

PHP code
  1. Array_filter (array, function) array is required. Specifies the input array. When the function does not have the second parameter, array_filter () deletes all entries in the array that are equivalent to FALSE. this is the essence of filtering the blank elements in the array.

For better understanding, paste the following online example:
 

PHP code
  1. Function myfunction ($ v)
  2. {
  3. If ($ v = "Horse ")
  4. {
  5. Return true;
  6. }
  7. Return false;
  8. }
  9. $ A = array (0 => "Dog", 1 => "Cat", 2 => "Horse ");
  10. Print_r (array_filter ($ a, "myfunction"); // output Array ([2] => Horse); note that the key value has not changed, if you want to re-sort the new array, you can use the sort function.

 


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.