note 104558 added to function.str-getcsv

From: Date: Thu, 23 Jun 2011 00:08:09 +0000
Subject: note 104558 added to function.str-getcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-180857@lists.php.net to get a copy of this message
Just another way to convert a csv file to an associative array. <? // // Convert csv file to associative array: // function csv_to_array($input, $delimiter='|') { $header = null; $data = array(); $csvData = str_getcsv($input, "\n"); foreach($csvData as $csvLine){ if(is_null($header)) $header = explode($delimiter, $csvLine); else{ $items = explode($delimiter, $csvLine); for($n = 0, $m = count($header); $n < $m; $n++){ $prepareData[$header[$n]] = $items[$n]; } $data[] = $prepareData; } } return $data; } //----------------------------------- // //Usage: $csvArr = csv_to_array(file_get_contents('test.csv')); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 85.16.71.176 ---- Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php Edit -- https://master.php.net/note/edit/104558 Del: integrated -- https://master.php.net/note/delete/104558/integrated Del: useless -- https://master.php.net/note/delete/104558/useless Del: bad code -- https://master.php.net/note/delete/104558/bad+code Del: spam -- https://master.php.net/note/delete/104558/spam Del: non-english -- https://master.php.net/note/delete/104558/non-english Del: in docs -- https://master.php.net/note/delete/104558/in+docs Del: other reasons-- https://master.php.net/note/delete/104558 Reject -- https://master.php.net/note/reject/104558 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#180857) next »