PHP array removal null value Array_filter () method

Source: Internet
Author: User

Function name: Array_filter ()
Call mode: Array_filter ($array)
Parameter description: $array is the object of operation, we will delete the empty element

Instance:

The code is as follows Copy Code

<?php
$array = (' A ' => ' abc ', ' B ' => ' BCD ', ' C ' => ' CDE ', ' d ' => ' def ', ' e ' => ');
Array_filter ($array);
echo "<pre>";
Print_r ($array);
?>

Results:

Array (

[A] => ABC

[b] => BCD

[C] => CDE

[d] => def

)

foreach or while, using these two syntactic constructs to delete the empty elements in the array, the simple code is as follows:

The code is as follows Copy Code

<?php
foreach ($arr as $k =&gt; $v) {
if (! $v)
Unset ($arr [$k]);
}
>

And I feel very good, but this is not high efficiency has been tried, first $arr into objects, and then take advantage of the characteristics of the object to delete, because: foreach is the current operation of the array into

Line copy, each operation of foreach, is a copy of a variable, the page if there are too many foreach, will be a very large consumption.

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.