Jump to content

Multidimensional Looping fgetcsv


viviosoft

Recommended Posts

I have data in a flat file that looks like the following:

 


#####  BEGIN PRODUCT LOOP #####

REF*19**OHIO VALLEY FLOORING
PID*F*TRN***SUPERINTENDENT
PID*F*MAC***CARINDR
MEA**LN*125.0*FT
MEA**WD*12.0*FT
MEA**SW*0.500*FP
CTP**PRICE1*9.790**SY****ST
CTP**PRICE2*11.190**SY****CT

####### BEGIN INTERNAL LOOP #######

G43*003**INVENTORY
SLN*1**O******SK*RKDA100AA591*****ST*A100A
PID*F*COLOR***SAGE TWEED
PID*F*35***A591

SLN*2**O******SK*RKDA100AA592*****ST*A100A
PID*F*COLOR***CREAMY TOFFEE
PID*F*35***A592

SLN*3**O******SK*RKDA100AA593*****ST*A100A
PID*F*COLOR***GALAXY GRAY
PID*F*35***A593

######## END INTERNAL LOOP ########

LIN**GS*A161*MF*ROOKWOOD CARPET*ST*A161
DTM*007*20110413

####### END PRODUCT LOOP #######

 

My problem is that the header information is on each line and not at the beginning of the file like most CSV formats.  Like the following:

 


REF*19 (Would be a column in the database)  --- I would need OHIO VALLEY FLOORING but for each Referenced SKU #
PID*F*TRN (Would be a column in the database)  --- I would need SUPERINTENDENT but for each Referenced SKU #
PID*F*MAC (Would be a column in the database)  --- I would need CARINDR but for each Referenced SKU #
and so on...

 

 

 

So the database output would look something like:

 

[/code]

 

SKU                          |  PRICE1          |  PRICE2    |  COLOR

---------------------------|-------------------|---------------|------------------

RKDA100AA591        |  9.790            |  11.190    |  SAGE TWEED

RKDA100AA592        |  9.790            |  11.190    |  CREAMY TOFFEE

RKDA100AA593        |  9.790            |  11.190    |  GALAXY GRAY

 

[/code]

 

How would I define each row in the data set and get the information for each row to stuff in the database?  Thanks for any help you can provide.  This is what I have at present:

 

print_r(buildStock('832x12Data.txt'));

function buildStock($File) {
        $handle = fopen($File, "r");
        $fields = fgetcsv($handle, 1000, ",");

        while($data = fgetcsv($handle, 1000, ",")) {
            $detail[] = $data;
        }

        $x = 0;
        $y = 0;

        foreach($detail as $i) {
            foreach($fields as $z) {
                $stock[$x][$z] = $i[$y];
                $y++;
            }
            $y = 0;
            $x++;
        }
        return $stock;
    }

 

 

 

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.