我有以下CSV:將csv解析爲關聯數組php;分隔符
稱號;標題2; TITLE3
測試,文本,文本2
test1的; text1的;文字3
我想將其轉換爲關聯數組,如:
[0]
title => test,
title2 => text
title3 => text2
[1]
title => test1,
title2 => text1
title3 => text3
我試着用:
$array = $fields = array(); $i = 0;
$handle = @fopen("file", "r");
if ($handle) {
while (($row = fgetcsv($handle, ";")) !== false) {
if (empty($fields)) {
$fields = $row;
continue;
}
foreach ($row as $k=>$value) {
$array[$i][$fields[$k]] = $value;
}
$i++;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
感謝
什麼這個問題你代碼?它輸出的是什麼? – hassan