PHP clears the empty value element in the array

Source: Internet
Author: User
Tags array empty functions string

For a one-dimensional PHP array, how do I erase an element whose value is empty? The direct method is a foreach loop, one judgment to exclude. But this method is slightly more complex, the following share a way to see today, very concise is the first time to see this writing, record.

Suppose that there is one one-dimensional array as follows:

$array =array (0=> ' Alixixi ',1=> ',2=> ' com ',3=> ');

Clearing the empty elements in the array can be written like this:

$array =array_filter ($array, create_function (' $v ', ' return!empty ($v); ");
Print_r ($array);
Output result: Array ([0] => Alixixi [2] => com);

Briefly analyze the two more important functions in the above code:

The array array_filter (array array,string function) function filters the elements in array arrays using callback function functions, and if the custom filter functions function returns True, The current value of the array being manipulated is included in the returned array of results, and the result is formed into a new array. If the original array is an associative array, the key name remains unchanged.

String Create_function (String $args, String $code) creates an anonymous 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.