How to add elements to the array using the foreach loop?
$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);
}
It seems like my problem is located at $list .=. How to insert another array into this array, so I can generate a .CSV file from the arrays?
$list[] = [x, y, z, ...];which is shorthand forarray_push()$list[] =. Using$list .=is for string concating.