PHP数组排序和文件导出问题

时间:2014-12-09 22:12:03

标签: php arrays sorting

所以我必须为作业制作一个PHP程序。任务是从.txt文件(学生ID,姓名,姓氏)中获取一些CSV数据。按字母顺序对数据进行排序,将其除以2并将数据放入2个代表2个研讨会类的文件中。我们不允许编辑原始数据。

我的代码有效,但有一些我正在努力修复的错误。以下是.txt文件的原始数据:

123, bill, bobs
124, joe, public 
125, amy, student
126, jane, doe
127, Ian, Dear
129, john,stonham
132, ali, mousavi
128, John, doe
130, bob, vinder
133, john, smith
134, joe, brown
138, jock, mcphelan
140, taffy, willians
145, harry, agius
146, rafiq,swash
157, angelina, karpovich
160, erica, elliott
162, steve, cockett
163, fred, weimer
165, jack, hass
167, paddy, o'reilly

以下是解决方案的代码:

    $students = array();

$groupNames1 = array();
$groupNames2 = array();

//Import and setup files for import and export
    $file_handle = fopen("students.txt", "r") or die("unable to open file!");
    $group1 = fopen("group_1.txt", "w") or die("Unable to create file!");
    $group2 = fopen("group_2.txt", "w") or die("Unable to create file!");

//Exploding csv format to extract contents into array
while (!feof($file_handle)) {
            $row = fgets($file_handle);
            $explode = explode(",", $row);
            $groupCountmportArray = array ('lName' => capitalise($explode[2]) , 'fName' => capitalise($explode[1]),  'id' => $explode[0]);

    $students[] = $groupCountmportArray;
}

fclose($file_handle);

//Arrange in alphabetical order
array_multisort($students);

//Divide array into 2 groups, sorting the remainder
$arrayDivide = round(sizeof($students)/2)-1;

//Checks to see if Group1 is the right size, if not adds another student until condition is met
$groupCount = 0;
while($groupCount < $arrayDivide){
            $groupNames1[] = $students[$groupCount];
            $groupCount++;
}

//Checks to see if Group2 is the right size, if not adds another student until condition is met
while($groupCount < sizeof($students)){
            $groupNames2[] = $students[$groupCount];
            $groupCount++;
}

//Adds Group1 students to the Group 1 file
$group1Write = 0;
while($group1Write < sizeof($groupNames1)){
            $txt = $groupNames1[$group1Write]['id'] . "," . $groupNames1[$group1Write]['fName'] . "," . $groupNames1[$group1Write]['lName'] ;
            fwrite($group1, $txt);
            $group1Write++;
}
fclose($group1);

//Adds Group2 students to the Group 2 file
$group2Write = 0;
while($group2Write < sizeof($groupNames2)){
            $txt = $groupNames2[$group2Write]['id'] . "," . $groupNames2[$group2Write]['fName'] . "," . $groupNames2[$group2Write]['lName'] ;
            fwrite($group2, $txt);
            $group2Write++;
}
fclose($group2);

//Capitalise the names in the file
function capitalise ($name){
    return ucwords($name);
}


//Displays message to let the user know that the code has run and the files have been created
echo "<h2>File Made. Please check your 'www' Folder on the C: Drive.</h2>";

这是输出:

文件1(工作正常):

145, Harry, Agius
123, Bill, Bobs
134, Joe, Brown
162, Steve, Cockett
127, Ian, Dear
126, Jane, Doe
128, John, Doe
160, Erica, Elliott
165, Jack, Hass
157, Angelina, Karpovich

文件2(有问题):

138, Jock, Mcphelan
132, Ali, Mousavi
167, Paddy, O'reilly124, Joe, Public 
133, John, Smith
125, Amy, Student
130, Bob, Vinder
163, Fred, Weimer
140, Taffy, Willians
129, John,Stonham
146, Rafiq,Swash

所以第二个文件的问题是Paddy O'Reilly和Joe Public在同一条线上。但是,因为John Stonham和Rafiq Swash位于原始逗号旁边,它正在影响数组的排序函数,因此输出错误。

对此的任何帮助都将非常感激,我已经对此持续了数小时,并且不知道转向哪种方式。

由于

1 个答案:

答案 0 :(得分:0)

输入文件显然不会以换行结束。如果该行在结尾处有换行符,则fgets()将返回以换行符结尾的字符串,但如果没有换行符,则不会添加换行符。所以你需要自己添加它。

要解决Stonham和Swash排序的问题,您需要排序而不包括空格。实现此目的的最简单方法是删除每个字段开头的空格。

while ($row = fgets($file_handle)) {
    if ($row[strlen($row)-1] != "\n") {
        $row .= "\n";
    }
    $groupCountmportArray = array (
        'lName' => capitalise(ltrim($explode[2])) , 
        'fName' => capitalise(ltrim($explode[1])),  
        'id' => $explode[0]
    );
    $students[] = $groupCountmportArray;
}