在PHP中将文本文件导入一个维度数组

时间:2016-11-04 12:59:03

标签: php arrays

我导入了一个文本文件,这里是代码:

<?php
$file = fopen("stoplist.txt","r") or die("fail to open file");
    $stoplist;
    $i = 0;
    while($row = fgets($file)){
        $data = explode(",", $row);
        $stoplist[$i] = $data;
        $i++;
    }
    fclose($file);

print "<pre>";
print_r($stoplist);
print "</pre>"


?>

它的输出如下:

Array
(
    [0] => Array
        (
            [0] => a

        )

    [1] => Array
        (
            [0] => able

        )

    [2] => Array
        (
            [0] => about

        )

    [3] => Array
        (
            [0] => above

        )
)

但我真的想要这样的结果:

Array
(
    [0] => a
    [1] => able
    [2] => about
    [3] => above
)

你对我的问题有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用array_merge将数组转换为单维数组:

$onedimension= call_user_func_array('array_merge', $stoplist);
print "<pre>";
print_r($onedimension);