Home  >  Article  >  Backend Development  >  How to convert table into array in php

How to convert table into array in php

PHPz
PHPzOriginal
2023-04-19 11:40:27529browse

In PHP language, it is very common to want to convert a table into an array. Usually, we need to process a large amount of data and read and operate it in the code. At this time, converting the table into an array will greatly facilitate our data processing and operation.

This article aims to introduce how to convert tables into arrays in PHP to help PHP enthusiasts easily process large amounts of data.

I. Convert CSV table to array

First of all, CSV (Comma-Separated Values) is a very common table form that can be generated with various tools and is easy to process. CSV tables only need to separate each column with commas and use carriage returns to separate each row, making it easier to read and operate.

The following is how to convert a CSV table into an array:

  1. Open the CSV file and read the data

In the following example, we will use fgets () function reads the contents of the CSV file and stores it line by line into the array $lines.

$csvFile = 'test.csv';

$lines = [];
if (($handle = fopen($csvFile, "r ")) !== FALSE) {

while (($data = fgets($handle)) !== FALSE) {
    $lines[] = $data;
}
fclose($handle);

}

?>

  1. Format CSV data and convert to array

Next, we will use a loop to traverse the $lines array, and use the explode() function to split each row of data into a new array according to commas, and finally obtain a two-dimensional array $csvData. You can use the count() function to get the number of rows and columns in the $csvData array.

$csvFile = 'test.csv';
$lines = [];
if (($handle = fopen($csvFile, "r")) !== FALSE) {

while (($data = fgets($handle)) !== FALSE) {
    $lines[] = $data;
}
fclose($handle);

$csvData = [];
foreach ($lines as $line) {
    $csvData[] = str_getcsv($line);
}

}

?>

  1. Output CSV data

Now, you can use var_dump () function sees the data in the $csvData array in the browser.

$csvFile = 'test.csv';
$lines = [];
if (($handle = fopen($csvFile, "r")) !== FALSE) {

while (($data = fgets($handle)) !== FALSE) {
    $lines[] = $data;
}
fclose($handle);

$csvData = [];
foreach ($lines as $line) {
    $csvData[] = str_getcsv($line);
}
var_dump($csvData);

}

?>

II. Convert Excel table to array

When we are not using CSV format, It is easier to convert Excel tables to array types. As follows:

  1. Install the PHPExcel plug-in

We can download this plug-in from the PHPEXCEL official website and extract it to the project directory.

require_once 'Classes/PHPExcel.php';
?>

  1. Read into Excel file

PHPExcel reads Excel files through the PHPExcel_IOFactory object. When using PHPExcel_IOFactory, a file reader is required to open Excel files. We need to upload PHP version 5.3 or above.

require_once 'Classes/PHPExcel.php';
$excel = PHPExcel_IOFactory::load('test.xlsx');
?>

  1. Convert Excel data to an array

Now that we have successfully read the Excel file into the $excel object, we can convert it into an array containing the data. First select the cell we need and get its value using the getValue() method in the PHPExcel_Cell class.

require_once 'Classes/PHPExcel.php';
$excel = PHPExcel_IOFactory::load('test.xlsx');
$sheet = $excel-> ;getActiveSheet();

$rows = [];
foreach ($sheet->getRowIterator() as $row) {

$item = [];
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);

foreach ($cellIterator as $cell) {
    $item[] = $cell->getValue();
}

$rows[] = $item;

}
?>

  1. Output Excel data

Finally, you can use the var_dump() function to view the data in the $rows array in the browser.

require_once 'Classes/PHPExcel.php';
$excel = PHPExcel_IOFactory::load('test.xlsx');
$sheet = $excel-> ;getActiveSheet();

$rows = [];
foreach ($sheet->getRowIterator() as $row) {

$item = [];
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);

foreach ($cellIterator as $cell) {
    $item[] = $cell->getValue();
}

$rows[] = $item;

}

var_dump ($rows);
?>

Summary

Through the above method, table data in different formats can be quickly converted into PHP array types. This method is especially suitable for processing large amounts of data, making data reading and processing more efficient and simpler. Therefore, this is a very important skill that is worth learning and mastering.

The above is the detailed content of How to convert table into array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn