Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jinsonjose's avatar

How to import xlsx file and convert to array using Maatwebsite\Excel

in Maatwebsite\Excel using how to convert xlsx file to convert to array

my question is i have a excel file and its data to be a array

$collection = Excel::toArray(new SectorFileImport, $path); but the header also consider as an array also in my excel contains only 16 rows one is header but array returns as 1024

result is like below

array:1 [▼ 0 => array:16 [▼ 0 => array:1025 [▼ 0 => "name" 1 => "address1" 2 => "address2" 3 => "address3" 4 => "dx_no" 5 => "title" 6 => "fore_name" 7 => "sur_name" 8 => "job_title" 9 => "phone" 10 => "email" 11 => "dx_exchange" 12 => "town" 13 => "county" 14 => "country" 15 => "pin_code" 16 => "website" 17 => "contact_title" 18 => "contact_first_name" 19 => "contact_last_name" 20 => "contact_address" 21 => "contact_phone" 22 => "contact_fax" 23 => "contact_email" 24 => "contact_postion" 25 => null 26 => null 27 => null 28 => null 29 => null 30 => null 31 => null 32 => null 33 => null 34 => null 35 => null 36 => null 37 => null 38 => null 39 => null 40 => null 41 => null 42 => null 43 => null 44 => null 45 => null 46 => null 47 => null 48 => null 49 => null 50 => null 51 => null 52 => null 53 => null 54 => null 55 => null 56 => null 57 => null 58 => null 59 => null 60 => null 61 => null 62 => null 63 => null 64 => null 65 => null

total result array

array:1 [▼ 0 => array:16 [▼ 0 => array:1025 [▶] 1 => array:1025 [▶] 2 => array:1025 [▶] 3 => array:1025 [ …1025] 4 => array:1025 [ …1025] 5 => array:1025 [ …1025] 6 => array:1025 [ …1025] 7 => array:1025 [ …1025] 8 => array:1025 [ …1025] 9 => array:1025 [ …1025] 10 => array:1025 [ …1025] 11 => array:1025 [ …1025] 12 => array:1025 [ …1025] 13 => array:1025 [ …1025] 14 => array:1025 [ …1025] 15 => array:1025 [ …1025] ] ]

any answer or suggestions??

0 likes
1 reply
avb's avatar

If your header is on the first row you can implement WithHeadingRow

Example from the docs:


use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class UsersImport implements ToModel, WithHeadingRow
{
    public function model(array $row)
    {
        return new User([
            'name'  => $row['name'],
            'email' => $row['email'],
            'at'    => $row['at_field'],
        ]);
    }
}

Please or to participate in this conversation.