如何删除没有数据或为空的下一行?

时间:2019-04-18 09:00:19

标签: php arrays laravel

我在使用laravel删除下一行空数据时遇到问题。我哪里出错了?

我尝试使用此代码爆炸,str_replace和array_map,但我仍然不想要我想要的东西。

$string = str_replace(array("\r", "\n"), "\n", $tempData[0]);
$your_arrays = array_map("trim", explode("\n", $string));

print_r ($your_arrays);

Array
(
    [0] => 
    [1] => 
    [2] => Sample Text1
    [3] => 
    [4] => Sample text 2
    [5] => Sample Text 3
    [6] => 
    [7] => 
    [8] => Sample Text 4
    [9] => Sample Text 5
    [10] => Sample Text 6
    [11] => Sample Text 7
    [12] => Sample Text 8
    [13] => 
    [14] => 
    [15] => 
)

我期望这样的结果

Array
(
    [1] => Sample Text1
    [2] => Sample text 2
    [3] => Sample Text 3
    [4] => Sample Text 4
    [5] => Sample Text 5
    [6] => Sample Text 6
    [7] => Sample Text 7
    [8] => Sample Text 8
)

4 个答案:

答案 0 :(得分:3)

使用array_filter删除所有

答案 1 :(得分:2)

您可以使用array_filter删除数组中的所有空条目。

答案 2 :(得分:2)

您可以使用array_filter()功能。

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

    print_r(array_filter($var));

答案 3 :(得分:0)

您可以简单地使用array_filter(),它为您方便地处理所有这一切:

print_r(array_filter($ Your_Array));