【问题标题】:php undefined variable when passing array in function [closed]在函数中传递数组时php未定义变量[关闭]
【发布时间】:2016-05-22 10:51:14
【问题描述】:

这里是新手。我正在尝试使用函数将 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

【问题讨论】:

    标签: php arrays function multidimensional-array


    【解决方案1】:

    你写的是 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];
    

    这就是它在函数内部工作的原因

    【讨论】:

    • 感谢您发现我的愚蠢错字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多