我正在对 PHP 进行 CSV 操作,我发现一个函数在 stackoverflow 上做得很好,除了它返回我的数组的值作为使数组难以操作的键。
array(6) {
[0]=>
array(2) {
["Aliens: Colonial Marines: Limited Edition (Xbox 360)"]=>
string(66) "Aliens: Colonial Marines: Limited Edition (Xbox 360) - EU Version"
["€ 14.34"]=>
string(8) "€ 7.17"
}
[1]=>
array(2) {
["Aliens: Colonial Marines: Limited Edition (Xbox 360)"]=>
string(46) "Arcania: Gothic 4 - Special Edition (Xbox 360)"
["€ 14.34"]=>
string(8) "€ 9.56"
}
[2]=>
array(2) {
["Aliens: Colonial Marines: Limited Edition (Xbox 360)"]=>
string(29) "Assassin's Creed 3 (Xbox 360)"
["€ 14.34"]=>
string(9) "€ 15.54"
}
[MORE]
}
有什么方法可以将数组更改为此以及如何更改??:
array(6) {
[0]=>
array(2) {
[0]=>
string(66) "Aliens: Colonial Marines: Limited Edition (Xbox 360) - EU Version"
[1]=>
string(8) "€ 7.17"
}
[1]=>
array(2) {
[0]=>
string(46) "Arcania: Gothic 4 - Special Edition (Xbox 360)"
[1]=>
string(8) "€ 9.56"
[MORE]
}
谢谢
编辑:
function parse_csv_file($csvfile) {
$csv = Array();
$rowcount = 0;
if (($handle = fopen($csvfile, "r")) !== FALSE) {
$max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000;
$header = fgetcsv($handle, $max_line_length);
$header_colcount = count($header);
while (($row = fgetcsv($handle, $max_line_length)) !== FALSE) {
$row_colcount = count($row);
if ($row_colcount == $header_colcount) {
$entry = array_combine($header, $row);
$csv[] = $entry;
}
else {
error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount");
return null;
}
$rowcount++;
}
//echo "Totally $rowcount rows found\n";
fclose($handle);
}
else {
error_log("csvreader: Could not read CSV \"$csvfile\"");
return null;
}
return $csv;
}