Excel2Array()
Overview
Excel2Array() reads an Excel spreadsheet file and converts its contents into a PHP array.
Each row in the spreadsheet becomes an element in the resulting array, and each column value becomes an element within that row.
This function is commonly used in MerciGlobal Cloud ERP for importing structured data such as products, customers, inventory records, or other bulk data from Excel files.
Syntax
Excel2Array($fExcel)
Parameters
| Parameter | Type | Description |
|---|---|---|
$fExcel |
string | The file path of the Excel file to read. The file must be accessible to the server. |
Return Value
Returns a multidimensional array representing the Excel worksheet data.
- Each outer array element represents a row.
- Each inner array element represents a column value.
Example structure:
[
["Column1", "Column2", "Column3"],
["Value1", "Value2", "Value3"],
["Value4", "Value5", "Value6"]
]
Example
Example Excel Sheet
| Product | Price | Qty |
|---|---|---|
| Pen | 10 | 50 |
| Pencil | 5 | 100 |
PHP Code
$file = "products.xlsx";
$data = Excel2Array($file);
print_r($data);
Output
[
["Product", "Price", "Qty"],
["Pen", "10", "50"],
["Pencil", "5", "100"]
]
Common Use Cases
- Importing product lists
- Bulk customer uploads
- Inventory updates
- Data migration from spreadsheets
- Preparing spreadsheet data for database insertion
Notes
- Ensure the Excel file exists before calling the function.
- Large Excel files may consume more memory and processing time.
- The function reads the sheet row by row.
- Empty cells may return an empty string or
nulldepending on the implementation.
Best Practices
Validate the file before processing and sanitize the imported data before inserting it into the database.
$file = "products.xlsx";
if(file_exists($file)){
$data = Excel2Array($file);
}
Related Functions
Array2Excel()– Converts a PHP array into an Excel file.CSV2Array()– Converts a CSV file into an array.ImportData()– Processes imported array data into ERP modules.