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
https://forums.phpfreaks.com/topic/234508-multidimensional-looping-fgetcsv/
Share on other sites

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.