文本到数组。结果中有奇怪的空行

时间:2014-06-18 19:37:45

标签: php

我有问题将文本更改为数组

这是文件中的文字:

 zkyu,team 3
 primesteval,team 2
 graves,team 3

我的剪辑;

$file = "test.txt";
$fh = fopen($file, 'r');
$members = array();
$toMoveMem = array();

$i = 0;
while (!feof($fh))
{
 $members[] = fgets($fh);   
}
fclose($fh);
foreach ($members as $Mem)
{
$MemS = explode(",", $Mem);
$a = ($MemS[0]);
$b = ($MemS[1]);
$c = str_replace("team", "", $b);
array_push($toMoveMem, array("MemberNick" => $a, "MemberTeam" => $c));
}

问题是数组键0和1中奇怪的空行:

   Array
    (
 [0] => Array
    (
        [MemberNick] => zkyu
        [MemberTeam] =>  3

    )

[1] => Array
    (
        [MemberNick] => primesteval
        [MemberTeam] =>  2

    )

[2] => Array
    (
        [MemberNick] => graves
        [MemberTeam] =>  3
    )

    )

无法弄清楚我做错了什么。 为什么数组键1和2看起来不像数组键3?

有人可以帮助我并解释一下。

1 个答案:

答案 0 :(得分:1)

我打赌它是因为在前两个对象之后有换行符。第三个没有一个,因为它是文件的结尾。您可以在trim()上使用$c

$c = trim(str_replace("team", "", $b));