PHP中去除数组中的空值的方法

PHP里面最重要的两章应该就是数组操作和字符串操作,这两章里面的函数都必须要熟练,其他的就等用的时候再查吧

方法1:以前在去掉数组的空值是都是强写foreach或者while的,利用这两个语法结构来删除数组中的空元素,简单代码如下

foreach( $arr as $k=>$v){   
    if( !$v )   
        unset( $arr[$k] );   
}  

只是这样效率不高,因为:foreach是将当前操作的数组进行copy,每操作一下foreach,都是copy了一个变量,页面里面如果有太多的foreach,会是一个很大的消耗

方法2:array_filter函数的功能是利用回调函数来对数组进行过滤,一直都以为用回调函数才能处理,却没有发现手册下面还有一句,如果没有回调函数,那么默认就是删除数组中值为false的项目


$entry = array(   
             0 => 'foo',   
             1 => false,   
             2 => -1,   
             3 => null,   
             4 => ''  
          );   
  
print_r(array_filter($entry)); 

方法3:array_diff函数的功能是计算数组的差集,它返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值。注意键名保留不变

$array = array("a","b","",null,"c");
$array = array_diff($array, array(null));
print_r($array);

总结这么多,欢迎补充!

my organization is jesse thorn by way of arranged this advice inside
how to lose weight fast Ask Roofers In Ottawa About Sub Contracting

New hues for spring include pale blue
christina aguilera weight lossWhat Could a Highly Organised Designer Handbag Collection Do for Your Mind
Cheap Marketing Tactic from The Motley Fool
cartola fc The fit is so pure

The 5 Most Intense Rites of Passage from Around the World
transformice metro paying for instructions

Signed fashion line to debut August 1
ddtank they tend to do so in a spectacular fashion

Just what the fuck is dadcore anyway
kinox -4-free an ancient vampire with powers beyond the usual list

Production Planning for Garment Manufacturing
movie2k So let’s say you receive an order from your customer

What To Wear and Not To Wear To A Rush
free hd porn GM’s new Sonic should be a strong contender in the subcompact space

Anime Today Talks Japanese Pop Culture With Patrick Macias
youporn posting your approach individuals through the new internet page

Magical World of Wholesale Ladies Clothing for Desired Business Results
large porn tube While silk can work well on a full figure
large porn tube
此条目发表在 网站开发 分类目录。将固定链接加入收藏夹。

评论功能已关闭。