簡體   English   中英

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;    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM