The php Method for filtering empty arrays and the method for filtering empty arrays by elements with an empty array are described in the following three examples: for, foreach, and array_filter. The following is an example.
Php Tutorial: Filter empty arrays and filter elements with empty Arrays
The following example shows how to filter empty Arrays: for, foreach, and array_filter.
*/
// Method 1 use array_filter to call our custom function to filter NULL values
Function clear ($)
{
Return $ a <> "";
}
$ Array = array ("", '', 1 );
$ Stt = array_filter ($ array, "clear ");
Print_r ($ stt );
/*
Output result
Array
(
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
)
Null Value filtered
*/
// Filter empty data and process it cyclically
$ Array = array ("", '', '2','', '', 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 Example 3: Use for instance
$ Tarray = array ('', '11','', '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 after filtering the empty array is
Array
(
[1] => 11
[3] => www.bkjia.com
[6] => cn.net
)
Note: The original tutorial on this site will indicate the source www.bkjia.com
*/