TSV/CVS to an associative array

Hello All,

It’s been a while since I’ve been on here and hope you are all doing well.

I’m having a bit of problem of creating an associative array from an TSV/CVS file upload. It’s a just something I’m trying to work on to improve upon what I’m able to do but it’s really gotten me frustrated.

I am able to build a multidimensional array from the TSV file I have, I just can’t figure out how to make it an associative one.

Below is my current function to build the multidimensional array. I’ve never really worked with any delimited files before so kind of banging my head against the all trying to get this to work.

If anyone could give me advice on how to associate the headers with the data they belong to I’d appreciate it.

Thanks,
Valandor

[php]public function tsv_to_array(){
//Open File
if(($handle = fopen($this->file, ‘r’)) !==FALSE){
//Set Parent For Multidimentional Array To 0
$nn = 0;
while(($data = fgetcsv($handle, 0, “\t”)) !== FALSE){
$num = count($data);
//Populate Multidimentional Array
for ($x=0; $x < $num -1; $x++){
$tsvarray[$nn][$x] = $data[$x];
}
$nn++;
}
//Close File
fclose($handle);
}
//Return Array
return($tsvarray);
}// End tsv_to_array [/php]

I finally found the answer to this. Now to really dig into the code and make sure I understand it all so I can replicate it easier in the future.

Thank you all for looking

[php]
$array = $fields = array();
$i = 0;
$handle = @fopen($this->file, “r”);
if ($handle) {
while (($row = fgetcsv($handle, 0, “\t”)) !== false) {
if (empty($fields)) {
$fields = $row;
continue;
}
foreach ($row as $k => $value) {
$array[$i][$fields[$k]] = $value;
}
$i++;
}
if (!feof($handle)) {
echo “Error: unexpected fgets() fail\n”;
}
fclose($handle);
}

}

}[/php]

Just for fun, maybe quicker and easier:

[php]$rows = file($this->file);
$fields = explode("\t", array_shift($rows));

foreach($rows as $row) {
$data = explode("\t", $row);
$array[] = array_combine($fields, $data);
}[/php]

Maybe str_getcsv() instead of explode if you need more flexibility.

The only problem i see is with this line - $fields = $row; You have $fields defined as an array, but you’re not entering any information into it, except for the first line of the file. It should be $fields[] = $row;

fgetcsv() parses the row into an array, so they have an array of field names. Still, I wouldn’t use all that code.

@AbraCadaver

I do appreciate your input tho with running your code in a quick test I do run into an issue - Warning: array_combine(): Both parameters should have an equal number of element

What I was needing was to take the headers out of a tsv/cvs file and build an association based on those headers.

So something like:

First Name;Last Name;Age;
John,Doe,30
Jane,Doe,27

Would turn into

array(
[0]=> array(
[First Name] => John,
[Last Name] =>Doe,
[Age] => 30
)
[1] => array(
[First Name] => Jane,
[Last Name] => Doe,
[Age] => 27
)
)

That’s exactly what my code does, assuming that tabs separate the column data as you showed in your code. The example text you posted shows headers separated by semicolons ; and data separated by commas , but I don’t know what your actual file looks like. You need to replace the explodes() with str_getcsv() and pass the proper delimiter.

Sponsor our Newsletter | Privacy Policy | Terms of Service