I'm trying to write an excel from a laravel controller where i have all the data in an array, and i managed to write it, but i have to format it now, this is the array that i have:
[2020-12-30 13:22:05] local.DEBUG: array (
0 =>
array (
0 => '1',
1 => '2',
),
1 =>
array (
0 => 'Test Name 1',
1 => 'Test Name 2',
),
2 =>
array (
0 => 'user',
1 => 'user2',
),
3 =>
array (
0 => '1',
1 => '2',
),
)
This is the way i'm creating the Excel:
Excel::create('Filename', function($excel) use ($budgets) {
$excel->sheet('Sheetname', function($sheet) use ($budgets) { //$budgets is the array i'm printing on Log
$sheet->fromArray($budgets);
});
})->export('xls');
And this is the way that my excel is printing it:
1 | 2 |
Test Name 1 | Test Name 2 |
user1 | user2 |
1 | 2 |
And the way i want to print it is:
Code | Name | User | Number |
---|---|---|---|
1 | Test Name 1 | user1 | 1 |
2 | Test Name 2 | user2 | 2 |
But i don't know how to achieve this. Can anyone help me with this?
//edits//
I added some code to re estructure the array, now i have this:
$users = ['Code', 'Name', 'User', 'Number'];
for ($i=0; $i<count($code); $i++){
array_push($users, array($code[$i], $name[$i], $user[$i], $number[$i]));
}
Log::debug($users);
And this is the Log:
[2020-12-30 15:17:40] local.DEBUG: array (
0 => 'Code',
1 => 'Name',
2 => 'User',
3 => 'Number',
4 =>
array (
0 => '1',
1 => 'Test Name 1',
2 => 'user1',
3 => '1',
),
5 =>
array (
0 => '2',
1 => 'Test Name 2',
2 => 'user2',
3 => '2',
),
)
But i'm getting this error:
[message] => Row `Code` must be array.
[class] => PHPExcel_Exception