我正在尝试合并从多行文本文件导入的数组元素,用逗号分隔:
$ cat input.txt
one,two,three,four
red,blue,green
human,klingon,dolphin
我想要得到的是一个包含 10 个项目的数组。我试过的代码是这样的:
<?php
$fa=file("input.txt");
$w=array();
foreach($fa as $combo) {
$w=array_merge($w,explode(",",$combo));
}
print_r($w);
?>
问题是,我似乎在行尾后得到回报:
Array
(
[0] => one
[1] => two
[2] => three
[3] => four
[4] => red
[5] => blue
[6] => green
[7] => human
[8] => klingon
[9] => dolphin
)
为什么那里有空格?我该如何摆脱它们?