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  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 41 - 40 = ?

 
 [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] wiesemann (Mark Wiesemann)
I tried with SDG 0.6.3 and 0.7.1, the behaviour is the same. If there are column names, they are outputted in the first (header) row. If this does not work for you, please provide a test script and re-open this bug. Thanks.
 [2006-07-20 12:45 UTC] wiesemann (Mark Wiesemann)
Okay, as already said via email, I mixed this up with the CSV renderer. The request is not bogus, therefore.
 [2006-07-20 13:22 UTC] wiesemann (Mark Wiesemann)
Thank you for your bug report. This issue has been fixed in the latest released version of the package, which you can download at http://pear.php.net/get/Structures_DataGrid_DataSource_CSV --- Thanks for the nice patch, I've used it with some minor differences (0 => null, 'headline' => 'header' [more consistent with the naming elsewhere in the SDG code]).