0

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
delv123
  • 136
  • 13

2 Answers2

1

You will could re-structure your array. To get the print you want array should look like:

$budget = [
    ['Code', 'Name', 'User', 'Number'],
    [1, 'Test Name 1', 'user1', 1],
    ...
];
korxz
  • 126
  • 1
  • 2
  • 11
  • I'm trying to do this now: $budgets = array('Code', 'Name', 'User', 'Number'); for ($i=0; $i – delv123 Dec 30 '20 at 18:12
  • 1
    Is you variable $code instance of an array? Also you named "header" array $budgets, but in for loop you are pushing into $users. – korxz Dec 30 '20 at 18:18
  • Sorry, yes, i made a mistake with the array names, it's the same users array that i'm declaring with the "headers". I'm going to edit the post, so you can see it more clearly – delv123 Dec 30 '20 at 18:21
0

Ok, so, i managed to re-structure the array and getting the Excel the way i wanted doing this:

public function generateExcel(Request $request)
    {
        $code = $request->input('code');
        $name = $request->input('name');
        $user = $request->input('user');
        $number = $request->input('number');

        $users = array();
        array_push($users, array('Code', 'Name', 'User', 'Number'));
        for ($i=0; $i<count($code); $i++){
            array_push($users, array($code[$i], $name[$i], $user[$i], $number[$i]));
        }
        
        
        Excel::create('Filename', function($excel) use ($users) {

            $excel->sheet('Sheetname', function($sheet) use ($users) {

                $sheet->fromArray($users);

            });

        })->export('xls');
        return true;
delv123
  • 136
  • 13