用新行php将数组内容放入字符串

时间:2017-01-05 12:52:08

标签: php arrays

我有一个文件,其中包含新行上的字符串,如下所示:

string1
string2

我想删除重复项,所以我将它们添加到数组中,执行唯一操作以删除重复项,现在我尝试删除数组并将内容放入字符串中,放回换行符(“\ n”)。

代码如下:

$input = file_get_contents('/srv/test.m3u');
$input = explode("\n", $input);

$result = array_unique($input);

foreach($result as $value){
    $newresult .= $value;
}

echo($newresult); //Comes back everything stuck together no line-breaks etc

2 个答案:

答案 0 :(得分:1)

使用implode添加“\ n”,如下所示:

$input = file_get_contents('/srv/test.m3u');
$input = explode("\n", $input);

$result = array_unique($input);
echo implode("\n", $result);

答案 1 :(得分:1)

尝试内爆功能。

$input = file_get_contents('/srv/test.m3u');
$input = explode("\n", $input);

$result = array_unique($input);

$newresult = implode("\n",$result);

echo($newresult);
相关问题