0
如何使用foreach循環向數組添加元素?PHP使用foreach()向數組添加元素
$list = [
['User-ID', 'Payout-ID', 'Amount', 'Withdraw address', 'Date'],
];
//generate CSV
foreach ($open_payouts as $open_payout) {
$list .= [
(string)$open_payout->user_id,
(string)$open_payout->id,
(string)number_format($open_payout->amount/100000000, 8, '.', ''),
(string)$open_payout->user->withdraw_address,
(string)$open_payout->created_at,
];
}
$fp = fopen(app_path() . '/CSV/file.csv', 'w');
//write whole list
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
看起來像我的問題是位於$list .=
。如何插入另一個數組到這個數組中,所以我可以從數組中生成一個.CSV文件?
'$列表[] = [X,Y,Z,...];'其是用於速記['array_push()'](http://php.net/manual/en/function .array-push.php) –
要將元素添加到數組中,它的'$ list [] ='。使用'$ list。='用於* string * concating。 – Qirel