我使用此代碼導入基本CSV,其中每一行代表發票。我希望這些發票按列之一分組,並最終執行json_encode來生成具有父/子層次結構的json。我如何去做這件事?結構CSV到對象/數組中
CSV
ID CardCode sum amount
165 BENV5271 100 100
026 BENV5635 509.85 287.33
9025 BENV5635 509.85 222.52
PHP
if (($handle = fopen('upload/BEN-new.csv', "r")) === FALSE) {
die('Error opening file');
}
$headers = fgetcsv($handle, 1024, ',');
$cardCodes = array();
while ($row = fgetcsv($handle, 1024, ",")) {
$cardCodes[] = array_combine($headers, $row);
}
fclose($handle);
JSON(目標)
[ {
"CardCode":"BENV5271",
"payment_sum": "100.00"
"details": [ {
"DocNum": "165",
"InvPayAmnt": "100.00",
"PmntDate": "2012-03-29"
} ],
}, {
"CardCode": "BENV5635",
"payment_sum": "509.85"
"details": [ {
"DocNum": "026"
"InvPayAmnt": "287.33",
"PmntDate": "2012-03-29"
}, {
"DocNum": "025",
"InvPayAmnt": "222.52",
"PmntDate": "2012-03-29"
} ],
} ]
您可以使用'json_encode($ row)'php inbuild函數 – Poonam
但是這不會創建層次結構。 – user1413248
SO不是關於讓其他人爲您寫代碼。如果你想要的話,你需要與某人簽約並付款。 – Crontab