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);

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.