Karem
Karem

Reputation: 18103

Excel to PHP array, possible and how?

I have a big excel file that looks like this:

enter image description here

I would like to put each row into an array.

Is this possible to access the first row's order id like this?

$result[0][2] // returns 7432

Assuming the actual first row that gives prefix for the columns' name is not present.

How could I do that?

Upvotes: 21

Views: 86214

Answers (5)

Haim Evgi
Haim Evgi

Reputation: 125526

I recommended to use PHPEXCEL library

https://github.com/PHPOffice/PHPExcel

you can see an example

Update: Now the alternative to this library is phpspreadsheet

Upvotes: 24

Saed Yousef
Saed Yousef

Reputation: 118

Since the PHPExcel library deprecated they've released "PhpSpreadsheet"

This will help PhpSpreadsheet

Upvotes: 2

paulalexandru
paulalexandru

Reputation: 9530

This works for me:

$content = file_get_contents($your_file_path); 
$lines = array_map("rtrim", explode("\n", $content));

Upvotes: 5

Rostyslav Pylypenko
Rostyslav Pylypenko

Reputation: 376

May be my answer is too simple (for one time work only), but I use the CONCATENATE "Function" in excell. The last cell on each row will have concatenation function, like this:

=CONCATENATE("['";A2;"'=>['data1' => '";B2;"', 'data2' => '";C2;"'],")

where:

column "A" is ID of something;
column "B" is first characteristic;
column "C" is second characteristic;
etc.

Then just copy and paste function results to Your script or config file, and do not forget the first and the last bracket.

Upvotes: 9

Dan
Dan

Reputation: 537

Save the spreadsheet as a CSV, then use PHP's built-in CSV functions. See the sample code here:

http://php.net/manual/en/function.fgetcsv.php

Upvotes: 19

Related Questions