我有一个 csv 文件中的数据,我使用以下方法将其组织成一个多维数组:
$handle = fopen("bankdata.csv", "r");
while(($data = fgetcsv($handle, 0 ,",")) !==FALSE) {
$transactions[] = $data;
}
数组现在看起来像这样:
Array
(
[0] => Array
(
[0] => 2000
[1] => paycheck
[2] => credit
)
[1] => Array
(
[0] => 75
[1] => grocery
[2] => debit
)
[2] => Array
(
[0] => 45
[1] => gas
[2] => debit
)
[3] => Array
(
[0] => 900
[1] => investments
[2] => credit
)
[4] => Array
(
[0] => 1500
[1] => bonus
[2] => credit
)
现在我想命名每个嵌套数组中的键。我想我会创建一个具有等量嵌套数组的新多维数组,它们的值是我想添加到原始数组中的键的预期名称,然后执行“array_combine”:
$names = array('amount','source','type');
$run = 1;
while($run < 6){
$run = $run +1;
$names2[] = $names;
}
$combine = array_combine($names2, $transactions);