Unset blank array items on the fly

℡╲_俬逩灬. 提交于 2019-12-14 03:16:10

问题


I have an array which is being built using:

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

The output is as follows:

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] => 
        )
)

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.


回答1:


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)

Edit: 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)

or this

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



回答2:


I understand you want to do this:

$array = array(null, null, 'Test', null, null);
$output = array_values(array_filter($array));
var_dump($output);

outputs:

array(1) {
  [0]=>
  string(4) "Test"
}

When no callback is passed to array_filter, it will only return values not evaluating to false. Values will be typecasted, so 0 is false, as is '' or null. Keep in mind though that the resulting array from your preg_split might contain control chars. If the resulting array var_dumps not as given above, try to change the pattern in preg_split to '/'.PHP_EOL.'/'.

Alson note that you either want to use the single preg_split with flags (as given in another answer) or array_values plus array_filter plus explode for performance reasons. Using array_values plus array_filter plus preg_split is 2 times slower than either of the other two alternatives, which approximately perform the same.


$str = <<< TXT

Test

Foo

Bar

TXT;

Testing with

$start = microtime(true);
for($i=0; $i<100000; $i++) {
    array_values(array_filter(explode(PHP_EOL, $str)));
}
echo microtime(true) - $start, PHP_EOL; // 0.60249495506287

$start = microtime(true);
for($i=0; $i<100000; $i++) {
    array_values(array_filter(preg_split("/".PHP_EOL."/", $str)));
}
echo microtime(true) - $start, PHP_EOL; // 1.0394451618195

$start = microtime(true);
for($i=0; $i<100000; $i++) {
    preg_split("/".PHP_EOL."/", $str, -1, PREG_SPLIT_NO_EMPTY);
}
echo microtime(true) - $start, PHP_EOL; // 0.60252904891968


来源:https://stackoverflow.com/questions/2317362/unset-blank-array-items-on-the-fly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!