note 85733 added to function.str-getcsv

From: Date: Mon, 15 Sep 2008 11:29:18 +0000
Subject: note 85733 added to function.str-getcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-144421@lists.php.net to get a copy of this message
For those who need this function but not yet installed in their environment, you can use my function bellow. You can parse your csv file into an associative array (by default) for each lines, or into an object. <?php function parse_csv($file, $options = null) { $delimiter = empty($options['delimiter']) ? "," : $options['delimiter']; $to_object = empty($options['to_object']) ? false : true; $str = file_get_contents($file); $lines = explode("\n", $str); pr($lines); $field_names = explode($delimiter, array_shift($lines)); foreach ($lines as $line) { // Skip the empty line if (empty($line)) continue; $fields = explode($delimiter, $line); $_res = $to_object ? new stdClass : array(); foreach ($field_names as $key => $f) { if ($to_object) { $_res->{$f} = $fields[$key]; } else { $_res[$f] = $fields[$key]; } } $res[] = $_res; } return $res; } ?> NOTE: Line number 1 of the csv file will be considered as header (field names). TODO: - Enclosure handling - Escape character handling - Other features/enhancements as you need EXAMPLE USE: Content of /path/to/file.csv: CODE,COUNTRY AD,Andorra AE,United Arab Emirates AF,Afghanistan AG,Antigua and Barbuda <?php $arr_csv = parse_csv("/path/to/file.csv"); print_r($arr_csv); ?> // Output: Array ( [0] => Array ( [CODE] => AD [COUNTRY] => Andorra ) [1] => Array ( [CODE] => AE [COUNTRY] => United Arab Emirates ) [2] => Array ( [CODE] => AF [COUNTRY] => Afghanistan ) [3] => Array ( [CODE] => AG [COUNTRY] => Antigua and Barbuda ) ) <?php $obj_csv = parse_csv("/path/to/file.csv", array("to_object" => true)); print_r($obj_csv); ?> // Output: Array ( [0] => stdClass Object ( [CODE] => AD [COUNTRY] => Andorra ) [1] => stdClass Object ( [CODE] => AE [COUNTRY] => United Arab Emirates ) [2] => stdClass Object ( [CODE] => AF [COUNTRY] => Afghanistan ) [3] => stdClass Object ( [CODE] => AG [COUNTRY] => Antigua and Barbuda ) [4] => stdClass Object ( [CODE] => [COUNTRY] => ) ) // If you use character | (pipe) as delimiter in your csv file, use: <?php $arr_csv = parse_csv("/path/to/file.csv", array("delimiter"=>"|")); ?> ==NSD== ---- Server IP: 202.158.52.162 Probable Submitter: 202.3.208.15 ---- Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php Edit -- https://master.php.net/note/edit/85733 Del: integrated -- https://master.php.net/note/delete/85733/integrated Del: useless -- https://master.php.net/note/delete/85733/useless Del: bad code -- https://master.php.net/note/delete/85733/bad+code Del: spam -- https://master.php.net/note/delete/85733/spam Del: non-english -- https://master.php.net/note/delete/85733/non-english Del: in docs -- https://master.php.net/note/delete/85733/in+docs Del: other reasons-- https://master.php.net/note/delete/85733 Reject -- https://master.php.net/note/reject/85733 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144421) next »