在這裏坐了幾個小時,但我看不出什麼問題。PHP陣列搜索和fgetcsv
我有1個文件: Gross_matching.csv:
Actionspiele,77
Strategiespiele,112
而且關鍵字 「Actionspiele,Strategiespiele」。
想要得到這個文件,然後在「Actionspiele」之後搜索並且想要得到「77 | 112」。但我得到「77 | 77」。有人知道爲什麼嗎?
<?php
if (!empty($articleData['keywords'])) {
$temp_dir = "...";
if (file_exists($temp_dir."gross_matching.csv")) {
$csv = array();
$file_gross = fopen($temp_dir."gross_matching.csv", 'r');
while (($result = fgetcsv($file_gross, ",")) !== false) {
$csv[] = $result;
}
fclose($file);
} else {
$articleData['attr_attr18'] .= " | File not found";
}
if (!empty($csv)) {
$string = '';
$keywords = explode(",", $articleData['keywords']);
if(is_array($keywords)) {
foreach($keywords as $key => $value) {
$pos = array_search($value, $csv);
$string .= $csv[$pos][1]."|";
}
if (!empty($string)) {
$articleData['attr_attr18'] = $string;
} else {
$articleData['attr_attr18'] .= " - String empty";
}
}
} else {
$articleData['attr_attr18'] .= " - csv empty";
}
} else {
$articleData['attr_attr18'] .= " - not Gross";
}
?>
也將搜索放入它自己的函數中。所有這些if和else的樹以及什麼都不是和temp在這裏,關鍵詞那裏只有阻礙。 – hakre
如果您添加更多的CSV數據,而不僅僅是兩個搜索信息,那將會很不錯,因爲還有很多事情仍然需要考慮,如重複的關鍵字等,您還應該爲您的數據添加預期輸出 – Baba