csvReader.php

<?php class csvReader { private $csv; private $keys; // SELECT AND OPEN THE CSV FILE public function __construct($getCSV) { $this->csv = fopen($getCSV, 'r'); $this->keys = fgetcsv($this->csv); rewind($this->csv); } // RETURN THE DATA BASED ON GIVEN LINE NUMBER AND COLUMN NAME public function getData($num, $ind) { $row = 0; while ($data = fgetcsv($this->csv)) { if ($row == $num) { rewind($this->csv); $data = array_combine($this->keys, $data); return $data[$ind]; } $row++; } } public function __destruct() { fclose($this->csv); } } ?>
// Open the target csv file
$csv = new csvReader("targetFile.csv");

// Return the data based on the given line number and column name
$csv->getData(38, 'phone')

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.