2
我有一個包含一些關鍵字的關聯數組,例如([0-50]
,[50-100]
)以及它們各自的值,如23
,443
。想要將關聯數組數據轉換爲CSV格式
我想要生成一個CSV格式的文件,我想在其中有兩列,如Distance
和Trips
。並且數組中的鍵必須位於Distance
的列中,並且數組中的值必須位於Trips
的列中。
我試過這個使用fputcsv()
但它沒有正確響應。
<?php
$file = fopen("/tmp/abc.csv", 'w');
fputcsv($file, array(
'Distance', 'Trips'
));
foreach($ed as $k=>$v) {
fputcsv($file, $v);
}
fclose($file);
$ ed迴應是這樣的。
Array ( [0-50] => 880 [50-100] => 5 [100-150] => 1 [550-600] => 1 )
你能告訴你試過嗎? – Will
$ file = fopen(「/ tmp/abc.csv」,'w'); fflcsv($ file,array('Distance','Trips')); foreach($ ed as $ k => $ v) { fputcsv($ file,$ v); } fclose($ file);其中$ ed是數組形式 - > Array([0-50] => 21 [50-100] => 45) –
請將$ ed添加到您的示例中 – Gavriel