PHP Filter Empty array method and filter array empty elements _php tutorial

Source: Internet
Author: User
PHP Filter Empty array method and filter array empty element filter empty array method I have three examples below, one is for,foreach,array_filter to handle, see the example below

PHP Tutorial Filter Empty array method and filter array to empty elements
Methods for filtering empty arrays I'm going to give you three examples, one for For,foreach,array_filter, and the example below.
*/

Method one uses Array_filter to invoke our custom function to filter the null value

function Clear ($a)
{
Return $a <> "";
}

$array = Array ("", ",", ",", ", 1,1,1,1,1);
$STT = Array_filter ($array, "clear");

Print_r ($STT);
/*
Output results

Array
(
[5] = 1
[6] = 1
[7] = 1
[8] = 1
[9] = 1
)
Null values are filtered
*/

Filter empty data Two, use loops to process

$array = Array ("", "', ' 2 '," ', ", 1,1,1,1,1);

foreach ($array as $v = $VC)
{
if ($VC = = ")
{
Unset ($array [$v]);
}
}

Print_

R ($array);

/*
Array
(
[2] = 2
[5] = 1
[6] = 1
[7] = 1
[8] = 1
[9] = 1
)
*/

Method instance three, using for to instance

$tarray = Array (' ', ' one ', ', ' www.bkjia.com ', ' ', ' ', ' cn.net ');

$len = count ($tarray);
for ($i =0; $i < $len; $i + +)
{
if ($tarray [$i] = = ")
{
Unset ($tarray [$i]);
}
}

Print_r ($tarray);

/*
The result of filtering an empty array is

Array
(
[1] = 11
[3] = www.bkjia.com
[6] = Cn.net
)
Note: This site original tutorial to indicate the source www.bkjia.com
*/


http://www.bkjia.com/PHPjc/444942.html www.bkjia.com true http://www.bkjia.com/PHPjc/444942.html techarticle PHP Filter Empty array method and filter Array empty element filter empty array method I have three examples below, one is for,foreach,array_filter to deal with, see the example PHP tutorial over ...

  • 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.