note 117631 deleted from function.fgetcsv by cmb

From: Date: Sun, 16 Jun 2019 08:12:45 +0000
Subject: note 117631 deleted from function.fgetcsv by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211270@lists.php.net to get a copy of this message
Note Submitter: suchbox1 at web dot de ---- read .csv file (with 1st row = head-row / key-names) .... to assoc array <?php function csv_to_array($csvfile, $csvhead=true, $delimiter=";", $enclosure='"', $rowlength=0) { // -------------------------------------- $csvdata = array(); // ---------------------------- if (file_exists($csvfile)) { $ReadHandle = @fopen($csvfile, 'r'); if (true === $csvhead) { $headrow = fgetcsv($ReadHandle, $rowlength, $delimiter, $enclosure); } // --------------------- while($line = fgetcsv($ReadHandle, $rowlength, $delimiter, $enclosure)) { if (true === $csvhead) { foreach ($headrow as $key => $heading) { if (!empty($heading)) { $row[$heading] = (isset($line[$key])) ? $line[$key] : ''; } } if (!empty($row)) { $csvdata[] = $row; } } else { if (!empty($line)) { $csvdata[] = $line; } } } // --------------------- @fclose($ReadHandle); } // ---------------------------- return $csvdata; // -------------------------------------- } ?> usage / example: --------------------------------------------------- $res_data1 = csv_to_array($filename1); $res_data2 = csv_to_array($filename2, true, '|'); $res_data3 = csv_to_array($filename3, true, ';', '"', 2400);

« previous php.notes (#211270) next »