mabart / parsecsv-for-php

Automatically exported from code.google.com/p/parsecsv-for-php
MIT License
0 stars 0 forks source link

Restructuring of the "heading" functionality #7

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Currently, parsecsv-for-php has a $heading parameter that will consume the
first line and use it as headers for each respective column. Unfortunately,
it seems to have lumped two distinct features together.

That is, not only is parseCSV::$titles populated with the first row, but
parseCSV::$data becomes an associative array. I believe that
parseCSV::$data should stay as an indexed array and a new parameter,
$associative_data, should be introduced to cover that respective functionality.

Of course, there's a workaround to this issue at present, but it means
having to manipulate both parseCSV::$heading as well as parseCSV::$offset.
I'd prefer to fix problems rather than fix symptoms, though. :)

Original issue reported on code.google.com by teh.a...@gmail.com on 7 Jul 2008 at 2:02

GoogleCodeExporter commented 8 years ago
With my limited understanding of the codebase, this seems to be effective with 
no
negative repercussions:

+ 90|  # build the data array with associative keys
+ 91|  var $assoc_key= false;
+ 92|   
-449|   $key = ( !empty($head[$col]) ) ? $head[$col] : $col ;
+449|   $key = ( !empty($head[$col]) && $this->assoc_key ) ? $head[$col] : $col ; 

Original comment by teh.a...@gmail.com on 7 Jul 2008 at 5:29

GoogleCodeExporter commented 8 years ago
Good point :)

However, at the moment I'm planning on implementing this with an option, rather 
than just filling one array 
with associative data, and one with indexed data, effectively consuming nearly 
twice the amount of memory 
otherwise used.

So by default, I'll probably leave parseCSV working as it is now, but with an 
option to enable a indexed array 
aswell simply using numbers for key names for each field in a row.

My main reason behind this approach right now is to keep things backwards 
compatible. However, I might 
endup doing something different once I start messing with the code.

I'll keep this issue updated at what I'll do :)

Original comment by zynode on 10 Jul 2008 at 6:47

GoogleCodeExporter commented 8 years ago
I'm not sure I understand what you're saying - the solution I posted doesn't 
create
an additional array, it just changes what key is used to initially store the 
data
into parseCSV::$data.

Original comment by teh.a...@gmail.com on 10 Jul 2008 at 8:54

GoogleCodeExporter commented 8 years ago
Damn, you're right. Not sure what I was thinking last night when I posted that. 
I knew +24 hours awake and 
trying to do stuff that requires brain cells was a bad idea... lol

Now that I've actually got a functioning head, your solution is pretty much 
exactly what I in mind, except I'm 
planning to have few different values of the assoc_key which will change how 
the data is output. As in make 
parseCSV::$data use key names, parseCSV::$index_data (example name only) use 
key numbers, or have the 
values in key names and numrical values both in the main array.

Also, currently you can access data more or less like the way you want without 
using offset. Simply parse the 
file as normal, and call $csv->data[$row][$csv->titles[2]]; to access the 3rd 
column. It's not as pretty as the 
final solution will be.

P.S. Sorry for being dumber than a 2 year old last night... lol :$

Original comment by zynode on 11 Jul 2008 at 5:21

GoogleCodeExporter commented 8 years ago
my CSV file not have title and your solution escape 1st row of file.

Original comment by mohamed....@gmail.com on 16 Dec 2014 at 1:38

GoogleCodeExporter commented 8 years ago
You can disable the heading/title feature of the parser by setting 
$csv->heading to false.

If you still have any issues, please report them on Github instead of here: 
https://github.com/parsecsv/parsecsv-for-php

Original comment by zynode on 16 Dec 2014 at 11:20