Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.1.6

Request #8252 Allow head line in CSV to produce nice associative array
Submitted: 2006-07-19 20:32 UTC
From: gramlich at eosc dot de Assigned: wiesemann
Status: Closed Package: Structures_DataGrid_DataSource_CSV (version 0.1.1)
PHP Version: 4.3.2 OS:
Roadmaps: (Not assigned)    
Subscription  


 [2006-07-19 20:32 UTC] gramlich at eosc dot de (Gregor Gramlich)
Description: ------------ It would be nice to have column names, i.e. string indices of an associative array, instead of numeric indices. I think it's not so unusual for CSV files to contain column names in the first line. The following patch handles this nicely, if you set the parameter $options = array('headline' => true) for the bind() method. This solution is BC. --- CSV.phpBAK 2006-07-19 12:08:27.127070240 +0200 +++ CSV.php 2006-07-19 12:33:09.843663056 +0200 @@ -63,10 +63,30 @@ $rowList = explode("\n", $csv); } + if (!empty($this->_options['headline'])) { + $keys = explode($this->_options['delimiter'], rtrim($rowList[0])); + unset($rowList[0]); + } else { + $keys = 0; + } + foreach ($rowList as $row) { $row = rtrim($row); // to remove DOSish \r if (!empty($row)) { - $this->_ar[] = explode($this->_options['delimiter'], $row); + if (empty($keys)) { + $this->_ar[] = explode($this->_options['delimiter'], $row); + } else { + $rowAssoc = array(); + $rowArray = explode($this->_options['delimiter'], $row); + foreach ($rowArray as $index => $val) { + if (!empty($keys[$index])) { + $rowAssoc[$keys[$index]] = $val; + } else { + $rowAssoc[$index] = $val; + } + } + $this->_ar[] = $rowAssoc; + } } }

Comments

 [2006-07-20 10:32 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [2006-07-20 12:45 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!
 [2006-07-20 13:22 UTC] User who submitted this comment has not confirmed identity
If you submitted this note, check your email.If you do not have a message, click here to re-send
MANUAL CONFIRMATION IS NOT POSSIBLE.  Write a message to pear-dev@lists.php.net
to request the confirmation link.  All bugs/comments/patches associated with this

email address will be deleted within 48 hours if the account request is not confirmed!