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


PHP Array sort and file export issue

所以我必须为作业制作一个PHP程序。任务是从.txt文件(学生证、姓名、姓氏(中获取一些 CSV 数据。按字母顺序对数据进行排序,将其一分为二,然后将数据放入代表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位于原文中的逗号旁边,它会影响数组的排序功能,从而使它们输出不正确。

对此的任何帮助将不胜感激,我已经为此苦苦挣扎了几个小时,不知道该转向哪种其他方式。

谢谢

输入文件显然不以换行符结尾。 如果行尾有换行符,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;
}