| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- /**
- * CsvReader class to read CSV files from a specified directory.
- * It reads all CSV files, combines their headers, and returns an array of rows.
- */
- class CsvReader {
- /**
- * Directory where CSV files are stored.
- * @var string
- */
- private $dir;
- /**
- * Constructor to initialize the CsvReader with a directory.
- *
- * @param string $directory The directory containing CSV files.
- */
- public function __construct($directory) {
- $this->dir = rtrim($directory, '/') . '/';
- }
- /**
- * Reads all CSV files in the specified directory and returns an array of rows.
- *
- * @return array An array of associative arrays representing the rows in the CSV files.
- */
- public function readAll() {
- $rows = array();
- foreach (glob($this->dir . '*.csv') as $file) {
- if (($h = fopen($file, 'r')) !== false) {
- $header = fgetcsv($h, 0, ',', '"', '');
- while (($data = fgetcsv($h, 0, ',', '"', '')) !== false) {
- $rows[] = array_combine($header, $data);
- }
- fclose($h);
- }
- }
- return $rows;
- }
- }
|