【发布时间】:2021-03-27 20:58:26
【问题描述】:
我有一个 csv 文件,其中有标题。第一列有一个唯一的 id。
我使用以下代码输出数组和标题作为键:
function csv_to_array($filename='', $delimiter=',') {
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
现在我得到了输出
Array
(
[0] => Array
(
[id] => 2548
[description ] => MyDescription
[value] => 5
)
[1] => Array
(
[id] => 2549
[description ] => MyDescription
[value] => 10
)
我想把“id”作为组数组的键
Array
(
[2548] => Array
(
[id] => 2548
[description ] => MyDescription
[value] => 5
)
[2549] => Array
但我之前不能调用 id 一个组。
【问题讨论】: