Home  >  Article  >  Backend Development  >  php 过滤空数组方法与过滤数组为空的元素_PHP教程

php 过滤空数组方法与过滤数组为空的元素_PHP教程

WBOY
WBOYOriginal
2016-07-20 11:07:34726browse

php 过滤空数组方法与过滤数组为空的元素 过滤空数组的方法我下面举了三个实例,一个是for,foreach,array_filter来处理,下面看实例

php教程 过滤空数组方法与过滤数组为空的元素
 过滤空数组的方法我下面举了三个实例,一个是for,foreach,array_filter来处理,下面看实例
*/

//方法一 利用array_filter调用我们自定的函数来过滤空值

function clear($a)
{
return $a "";
}

$array = array("",'','','','',1,1,1,1,1);
$stt = array_filter($array , "clear");

print_r( $stt );
/*
输出结果

Array
(
    [5] => 1
    [6] => 1
    [7] => 1
    [8] => 1
    [9] => 1
)
空值被过滤
*/

//过滤空数据二,用循环来处理

 

$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
)
*/

//方法实例三,用for来实例

$tarray = array('','11','','www.bkjia.com','','','cn.net');

$len = count( $tarray );
for( $i=0;$i{
 if( $tarray[$i] == '' )
 {
  unset( $tarray[$i]);
 }
}

print_r($tarray);

/*
过滤空数组后的结果为

Array
(
    [1] => 11
    [3] => www.bkjia.com
    [6] => cn.net
)
注明:本站原创教程转注明来源www.bkjia.com
*/


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444942.htmlTechArticlephp 过滤空数组方法与过滤数组为空的元素 过滤空数组的方法我下面举了三个实例,一个是for,foreach,array_filter来处理,下面看实例 php教程 过...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn