Jump to content

[SOLVED] csv file to array .. got myself stuck


severndigital

Recommended Posts

ok .. so i have several csv files i want to read into an array to work with.

 

my ideal solution would have the array look like this using print_r($array)

 

Array(Line_1_field1 => Line_1_field2,Line_2_field1 => Line_2_field2,...)and so on.

 

i can get the items out of the csv file and into an array but the array looks like this.

 

Array ( [0] => Array ( [productid] => DOC001 [qty] => 1 ) [1] => Array ( [productid] => DOC002 [qty] => 5 ) 

 

which would work however I can't seem to figure out how to extract specifically the productid value and the qty value from this array.

 

I know this is gonna be an easy one ... i just can't see it.

 

Any suggestions.

 

Thanks,

-C

 

here is the code i am using to produce the bottom result.

 

$filename = 'testfile.csv';

$f = @fopen($filename,'r');
if (!$f) return false;
$headers = fgetcsv($f,8090);
$all = array();
while (!feof($f)){
  $values = fgetcsv($f,8098);
  $row = array();
  $i=0;
  if (is_array($values)){
     foreach($values as $v){
        if ($i < count($headers)) $row[$headers[$i]] = $v;
      $i++;
     }
     $all[] = $row;
    }
}

print_r($all);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.