我似乎无法找到答案。有没有一种简单的方法可以将代码中的数组更改为关联数组?
注意: - 已经分配了所有已分配的变量 - 它基本上从一个名为staff数据的csv文件创建一个数组,然后循环查找下一个空行,然后将该行添加到数据中,然后重新保存回csv文件。 我的问题是,我无法弄清楚如何将这个数组更改为仍然可以使用我的代码的关联数组。任何方向都将非常感激。我希望我的问题很清楚。
// Grabs the csv file (and its existing data) and makes it into an array so the new data can be added to it.
$StaffDetails = array();
$lines = file('data/StaffData.csv', FILE_IGNORE_NEW_LINES);
foreach ($lines as $key => $value)
{
$StaffDetails[$key] = str_getcsv($value);
}
//This is a for loop that checks when there is an avaliable line to put the data into. It loops over the lines, until there is an empty spot for the new data //to go into.
//It will not run it if the full name is empty.
for ($i=0; $i<200; $i++) {
if(empty($StaffDetails[$i][0]) == false){ //If its full
}else if (empty($StaffDetails[$i][0]) == true){ //If it is empty
//This prints the details of this staff to that line in the data file. $i is the avaliable line found above.
$StaffDetails[$i][0] = $UserFullName;
$StaffDetails[$i][1] = $UserPhoneNumber;
$StaffDetails[$i][2] = $Gender;
$StaffDetails[$i][3] = $Birthday;
$StaffDetails[$i][4] = $TypeOfWork;
$StaffDetails[$i][5] = $StartingDate;
$StaffDetails[$i][6] = $AnnualLeaveLeft;
$StaffDetails[$i][7] = $SickLeaveLeft;
$StaffDetails[$i][8] = $ID;
$StaffDetails[$i][9] = $NumberOfWorkDaysAWeek;
$StaffDetails[$i][10] = $Monday;
$StaffDetails[$i][11] = $Tuesday;
$StaffDetails[$i][12] = $Wednesday;
$StaffDetails[$i][13] = $Thursday;
$StaffDetails[$i][14] = $Friday;
$StaffDetails[$i][15] = $Saturday;
$StaffDetails[$i][16] = $Sunday;
$StaffDetails[$i][17] = $UserUsername;
$StaffDetails[$i][18] = $UserPassword;
$StaffDetails[$i][19] = $Em1FullName;
$StaffDetails[$i][20] = $Em1PhoneNumber;
$StaffDetails[$i][21] = $Em2FullName;
$StaffDetails[$i][22] = $Em2PhoneNumber;
$i =201;
}
//Below saves the previous data and new changes to the csv file
//This opens the csv file as a write file
$MyCsvFile = fopen('data/StaffData.csv', 'w');
//This takes the array that has had data ddded to it and adds it to the file
foreach ($StaffDetails as $fields) {
fputcsv($MyCsvFile, $fields);
}
//This closes the file
fclose($MyCsvFile);
}