我的CSV文件具有这种结构CSV到PHP关联数组
title;firstName;lastName;email;
Sir;Bob;Public;[email protected];
第一行显示的列名。内容从第二行开始。现在,我想这放入关联数组这样
array([0] =>
array(
'title' => 'Sir',
'firstName' => 'Bob',
'lastName' => 'Public',
'email' => '[email protected]'
)
)
我试了一下:
$entities = array();
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
$line = array();
//save first row
if ($row == 1) {
foreach (explode(';', $data[$c]) as $field) {
//dont save empty fields
if ($field != "") {
$fields[] = $field;
$line[$field] = array();
}
}
}
//go through contents and save them
foreach ($fields as $field) {
for ($i = 2; $i < count($fields); $i++) {
$cust = explode(";", $data[$c]);
$line[$field][] = $cust[$i];
}
}
$entities[] = $line;
$row++;
}
}
fclose($handle);
}
这给没有错误,但是,这似乎是搞砸了一个怪阵。任何想法如何能得到我想要的结构?
你有一个';'分隔的文件还没有要指定的','作为分隔符。 –
谢谢,我编辑了我的问题 –
[CSV到关联数组]的可能重复(http://stackoverflow.com/questions/4801895/csv-to-associative-array) – ManseUK