PHP函数过滤.csv文件显示


PHP function to filter .csv file for display

我已经搜索了其他问题来让我走到这一步,但我错过了一个关键元素,使我的代码工作。

csv文件格式为:

job;customer;location;tech;side
Ex(12345;company;floor 1;John;Auto)

下面是我当前的函数:

function readCSV($csvFile,$side){
$file_handle = fopen($csvFile,'r');
while (!feof($file_handle)) {
    $line_of_text[] = fgetcsv($file_handle, 1024,';');
}
fclose($file_handle);
return $line_of_text;
}

我希望函数只加载与$side parm匹配的记录。(例如:驾驶侧,或自动侧)

提前感谢,我对从csv中收集数据是新手。

side是CSV的第五列(索引4),因此只需检查它是否等于$side,如果等于,则将其添加到数组中:

function readCSV($csvFile,$side){
    $file_handle = fopen($csvFile,'r');
    while (!feof($file_handle)) {
        //save temp line
        $line = fgetcsv($file_handle, 1024,';');
        //compare temp line forth column and if matches add line to array
        if ($line[4] == $side) {
            $line_of_text[] = $line;
        }
    }
    fclose($file_handle);
    return $line_of_text;    
}