在函数

时间:2016-02-11 15:17:12

标签: php arrays function multidimensional-array

新手在这里。我尝试使用函数将CSV文件读入二维数组,然后将该数组作为函数返回。一切似乎都有效,除了$ cvs [" CourseNumber"] [$ n]在我尝试在函数外使用它时给了我Undefined变量。如果我在设置之后在函数内打印$ cvs [" CourseNumber"] [$ n],则在函数内打印正确的值,但仍然在函数外部出错。

function parsecsv($csvfile)
{
if (($handle = fopen($csvfile, "r")) !== FALSE)
{
    $csv["rows"]=0;
    $n = 0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
        $csv["CourseTitle"][$n] = $data[0];
        $csv["OverallStartDate"][$n] = $data[1];
        $csv["OverallEndDate"][$n] = $data[2];
        $csv["OverallCourseDate"][$n]=$data[3];
        $cvs["CourseNumber"][$n]=$data[4];
        $csv["CourseName"][$n]=$data[5];
        $csv["CourseFacilitator"][$n]=$data[6];
        $csv["SurveyURL"][$n]=$data[7];
        $n++;
    }
    $csv["rows"]= $n;
    return $csv;
}
else
{
    print "Error: file not found";
    return 0;
}

}

$csv = parsecsv("test.csv");
print "<h1>From Function</h1><br />\n";

for ($n=0; $n < $csv["rows"]; $n++)
{
print "CourseTitle:" . $csv["CourseTitle"][$n] . "<br />\n";
print "OverallStartDate:" . $csv["OverallStartDate"][$n] . "<br />\n";
print "OverallEndDate:" . $csv["OverallEndDate"][$n] . "<br />\n";
print "OverallCourseDate:" . $csv["OverallCourseDate"][$n] . "<br />\n";
print "CourseNumber:" . $cvs["CourseNumber"][$n] . "<br />\n";
print "CourseName:" . $csv["CourseName"][$n] . "<br />\n";
print "CourseFacilitator:" . $csv["CourseFacilitator"][$n] . "<br />\n";
print "SurveyURL:" . $csv["SurveyURL"][$n];
print "<hr />";
}

CSV文件:

CourseTitle,OverallStartDate,OverallEndDate,OverallCourseDate,CourseNumber,CourseName,CourseFacilitator,SurveyURL
ELS,15-Nov-15,21-Nov-15,16-Nov-15,1,MAJCOM Commander,Gen Ellen Pawlikowski,link.url?066C6780080951712
ELS,15-Nov-15,21-Nov-15,16-Nov-15,2,Strategic Planning and Execution,Prof. Friga,link.url?647537E7780951713
ELS,15-Nov-15,21-Nov-15,17-Nov-15,3,Individual Decision-Making,Prof. Hofmann,link.url?F93104BEC80951711
ELS,15-Nov-15,21-Nov-15,17-Nov-15,4,Group Decision-Making,Prof. Hofmann,link.url?8F2153A8B80951710
ELS,15-Nov-15,21-Nov-15,18-Nov-15,5,Strategic Innovation,Prof. Segars,link.url?BC5904AB880951709
ELS,15-Nov-15,21-Nov-15,18-Nov-15,6,Motivating Others for High Performance,Prof. Pearsall,link.url?6456DC75980951708
ELS,15-Nov-15,21-Nov-15,19-Nov-15,7,Negotiation and Collaboration,Prof. Fragale,link.url?D7AD27F7D80951707
ELS,15-Nov-15,21-Nov-15,19-Nov-15,8,Financial Resource Management/Defense Industry Perspective,Prof. Connolly,link.url?D48ED1CCC80951706
ELS,15-Nov-15,21-Nov-15,20-Nov-15,9,Leading Change,Prof. Miguel,link.url?768FB940680951705
ELS,15-Nov-15,21-Nov-15,21-Nov-15,10,Ethics: The Consequences of Power,Prof. Fragale,link.url?B025508D180951703
ELS,15-Nov-15,21-Nov-15,21-Nov-15,11,Senior Faculty Advisor,Gen (ret) John Corley,link.url?EF210763A80951704
ELS,15-Nov-15,21-Nov-15,End-of-Course,12,End-of-Course Survey,Various Instructors,link.url?66E3F1DF480951702

1 个答案:

答案 0 :(得分:0)

您已经编写了cvs而不是csv

修正此行

print "CourseNumber:" . $cvs["CourseNumber"][$n] . "<br />\n";

print "CourseNumber:" . $csv["CourseNumber"][$n] . "<br />\n";

同样在你的函数parsecsv

中解决这个问题
$cvs["CourseNumber"][$n]=$data[4];

$csv["CourseNumber"][$n]=$data[4];

这是它在函数

中起作用的原因