note 117631 added to function.fgetcsv

From: Date: Sun, 12 Jul 2015 11:57:53 +0000
Subject: note 117631 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-203083@lists.php.net to get a copy of this message
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); ---- Server IP: 72.52.91.14 Probable Submitter: 188.192.70.69 ---- Manual Page -- http://php.net/manual/en/function.fgetcsv.php Edit -- https://master.php.net/note/edit/117631 Del: integrated -- https://master.php.net/note/delete/117631/integrated Del: useless -- https://master.php.net/note/delete/117631/useless Del: bad code -- https://master.php.net/note/delete/117631/bad+code Del: spam -- https://master.php.net/note/delete/117631/spam Del: non-english -- https://master.php.net/note/delete/117631/non-english Del: in docs -- https://master.php.net/note/delete/117631/in+docs Del: other reasons-- https://master.php.net/note/delete/117631 Reject -- https://master.php.net/note/reject/117631 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203083) next »