在PHP中导入文本文件到一维数组


Import text file into one dimentional array in PHP

导入一个文本文件,代码如下:

<?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
)
你对我的问题有什么建议吗?

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

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