使用下面的 php,我可以成功地将我的 CSV 文件读入关联数组,将“名称”/值分配给变量键。
<?php
$filepath = "./CSV.csv";
$file = fopen($filepath, "r") or die("Error opening file");
$i = 0;
while(($line = fgetcsv($file)) !== FALSE) {
if($i == 0) {
$c = 0;
foreach($line as $col) {
$cols[$c] = $col;
$c++;
}
} else if($i > 0) {
$c = 0;
foreach($line as $col) {
$data[$i][$cols[$c]] = $col;
$c++;
}
}
$i++;
}
?>
CSV 文件包含:
名称, 街道, 镇, 州, 邮编, 状态
名称 1,街道 1,城镇 1,州 1,邮编 1,状态 1
名称 2,街道 2,城镇 2,州 2,邮编 2,状态 2
名称 3,街道 3,城镇 3,州 3,邮编 3,状态 3
名称 4,街道 4,城镇 4,州 4,邮编 4,状态 4
名称 5,街道 5,城镇 5,州 5,邮编 5,状态 5
PHP的输出相应地:
Array
(
[1] => Array
(
[name] => name 1
[street] => street 1
[town] => town 1
[state] => state 1
[zip] => zip 1
[status] => status 1
)
[2] => Array
(
[name] => name 2
[street] => street 2
[town] => town 2
[state] => state 2
[zip] => zip 2
[status] => status 2
)
[3] => Array
(
[name] => name 3
[street] => street 3
[town] => town 3
[state] => state 3
[zip] => zip 3
[status] => status 3
)
[4] => Array
(
[name] => name 4
[street] => street 4
[town] => town 4
[state] => state 4
[zip] => zip 4
[status] => status 4
)
[5] => Array
(
[name] => name 5
[street] => street 5
[town] => town 5
[state] => state 5
[zip] => zip 5
[status] => status 5
)
)
=== 输出结束 ===
问题:
我如何为每个数组分配一个值以使其不提供:
Array
(
[1] => Array
(
...
)
[2] => Array
(
...
)
[3] => Array
(
...
)
[4] => Array
(
...
)
[5] => Array
(
...
)
)
但它会提供这样的东西:
Staff List
(
[1] => Director
(
...
)
[2] => Department Head
(
...
)
[3] => Head of FInance
(
...
)
[4] => Human Resource
(
...
)
[5] => etc
(
...
)
)
感谢您的意见 !