即时取消设置空白数组项目 [英] Unset blank array items on the fly

查看:73
本文介绍了即时取消设置空白数组项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下方法构建的数组:

I have an array which is being built using:

$features_data[$i] = preg_split("/\n/", $row);

输出如下:

Array
(
    [0] => Array
        (
            [0] =>   
            [1] =>   
            [2] =>                 <img src="http://example.com/sites/test/files/imagecache/feature-image/testimage_0.jpg" alt="" title=""  class="imagecache imagecache-feature-image imagecache-default imagecache-feature-image_default" width="654" height="260" />      
            [3] => 
            [4] =>   
            [5] =>   
            [6] =>                 Test 1      
            [7] => 
            [8] =>   
            [9] =>   
            [10] =>                 Lorem ipsum dolor sit...      
            [11] => 
            [12] => 
        )
    [1] => Array
        (
            [0] =>   
            [1] =>   
            [2] =>                       
            [3] => 
            [4] =>   
            [5] =>   
            [6] =>                 Test 2      
            [7] => 
            [8] =>   
            [9] =>   
            [10] =>                 Aenean id tellus nec...      
            [11] => 
            [12] => 
        )
    [2] => Array
        (
            [0] =>   
            [1] =>   
            [2] =>                       
            [3] => 
            [4] =>   
            [5] =>   
            [6] =>                 Test 3      
            [7] => 
            [8] =>   
            [9] =>   
            [10] =>                 Maecenas ut pharetra...      
            [11] => 
            [12] => 
        )
)

我想摆脱空白的数组项,然后重置数组计数器。我尝试使用未设置的php,但由于某种原因,它无法正常工作。任何帮助将不胜感激。

I'd like to get rid of the blank array items and then reset the array counter. I've tried using php unset but for some reason it's not working. Any help would be greatly appreciated.

推荐答案

您可以使用 array_filter() ,但您真正要使用的是 PREG_SPLIT_NO_EMPTY 代替。

You could use array_filter() but what you really want to use is PREG_SPLIT_NO_EMPTY instead.

preg_split("/\n/", $row, -1, PREG_SPLIT_NO_EMPTY)

编辑:当然,这完全取决于您的输出。您的模式可能应该是这样的

of course it entirely depends on your output. Your pattern should probably be something like this

preg_split("/[\\n\\r \\t]+/", $row, -1, PREG_SPLIT_NO_EMPTY)

或此

preg_split("/[\\n\\r \\t]*\\n[\\n\\r \\t]*/", $row, -1, PREG_SPLIT_NO_EMPTY)

这篇关于即时取消设置空白数组项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆