【发布时间】:2012-12-20 14:42:28
【问题描述】:
在这里坐了几个小时,但我看不出问题出在哪里。
我有 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 以及 what not 和 temp 的树,那里的关键字只是阻碍。
-
添加更多 CSV 数据而不是仅添加两个搜索信息会很好,因为仍然需要考虑很多事情,例如重复的关键字等。您还应该为数据添加预期输出跨度>
标签: php arrays search matching fgetcsv