Jump to content

Looping through array


viviosoft

Recommended Posts

Hello all.

 

How would I loop through this array and find each "DTM" value?  I want to be able to loop thru this array so that I can determine when to add 123, 456, ... 789, 1012 ... 123, 456 to a database.  Hope this makes some sense... ;)  Thanks for any help on this one.

 

<?php

$aryValues = array('DTM', '123', '456', 'DTM', '789', '1012', 'DTM', '123', '456');

?>

Link to comment
Share on other sites

Okay, here's a bit more information on my problem.  I have a flat file that has some data in it...  it looks like this.

 


DTM*007*20110413
PID*F*TRN***LAKE TAHOE 12FT
PID*F*MAC***CARINDR
MEA**LN*125.0*FT
MEA**WD*12.0*FT
MEA**SW*0.333*FP
SLN*1**O******SK*RKDAB408001*****ST*A08
PID*F*73***LIGHTHOUSE
PID*F*35***001
SLN*2**O******SK*RKDAB408002*****ST*A08
PID*F*73***COOKIE DOUGH
PID*F*35***002
SLN*3**O******SK*RKDAB408003*****ST*A08
PID*F*73***WOOD SAGE
PID*F*35***003
SLN*4**O******SK*RKDAB408004*****ST*A08
PID*F*73***CAFÉ LATTE
PID*F*35***004

DTM*007*20110413
PID*F*TRN***RATIO 12FT
PID*F*MAC***CARINDC
MEA**LN*125.0*FT
MEA**WD*12.0*FT
MEA**SW*0.421*FP
SLN*1**O******SK*RKDA1010255*****ST*A10
PID*F*73***PROPORTION
PID*F*35***10255
SLN*2**O******SK*RKDA1010351*****ST*A10
PID*F*73***EQUATION
PID*F*35***10351
SLN*3**O******SK*RKDA1010356*****ST*A10
PID*F*73***QUOTA
PID*F*35***10356
SLN*4**O******SK*RKDA1010451*****ST*A10
PID*F*73***RATE
PID*F*35***10451
SLN*5**O******SK*RKDA1010455*****ST*A10
PID*F*73***FRACTION
PID*F*35***10455
SLN*6**O******SK*RKDA1010458*****ST*A10
PID*F*73***PERCENTAGE
PID*F*35***10458
SLN*7**O******SK*RKDA1010555*****ST*A10
PID*F*73***DIVISION
PID*F*35***10555
SLN*8**O******SK*RKDA1010556*****ST*A10
PID*F*73***STANDARD
PID*F*35***10556
SLN*9**O******SK*RKDA1010751*****ST*A10
PID*F*73***QUALITY
PID*F*35***10751
SLN*10**O******SK*RKDA1010752*****ST*A10
PID*F*73***SCALE
PID*F*35***10752
SLN*11**O******SK*RKDA1010755*****ST*A10
PID*F*73***FORMULA
PID*F*35***10755
SLN*12**O******SK*RKDA1010756*****ST*A10
PID*F*73***MEASUREMENT
PID*F*35***10756

 

Each DTM is a "loop" in the data set.  I want to loop through the data set and put in the database the data within each DTM loop (Date Time Stamp).  Also including the DTM value.  The code I'm using is attached along with the flat data file.  The attached should give you a GOOD idea of the direction I'm trying to move in.  In the first loop "cleans" up the data file.  Then I want to take that cleaned up output array and loop through it and perform an insert into database. 

 

Also, I really don't need to finial output file.  That's mostly for my testing.  The end result will be inserting the information into a database once the data has been "cleaned" up.  My other hurdle is not every line is a field in the database.  The fields are as follows: DTM*007*,  MEA**LN*, MEA**WD*, SLN*9**O******SK*, PID*F*73***...  where it starts getting tricky is the multiple SLN's and PID's.  Obviously, the fields will be called something a bit more readable but I think you get the idea?  I hope :) If not, please let me know, I've been struggling with how to approach this.

 

Thanks for any help on this guys!

 

[attachment deleted by admin]

Link to comment
Share on other sites

file can get you an array of lines in the file, and trim can remove the line endings that file() leaves intact.

strncmp can compare the first N characters of two strings, like

if (strncmp($line, "DTM*007*",  == 0) {
    // $line starts with DTM*007*
}

With substr you can grab any substring if you know the start and end/length of the substring you want.

 

Couple questions:

1. Is that data the original data or the "cleaned" data?

2. what exactly do you want to do with the first block of data? What values should be extracted? What queries should be executed?

Link to comment
Share on other sites

Thanks for the reply requinix,  This post might be a bit more clear.  This is one I posted this morning. http://www.phpfreaks.com/forums/index.php?topic=331569.0

 

 

Couple questions:

1. Is that data the original data or the "cleaned" data?

2. what exactly do you want to do with the first block of data? What values should be extracted? What queries should be executed?

 

 

It might help answer the questions you asked in your reply.  Also, this data is the "cleaned" array but the post above has data that has not been cleaned.  You might want to reply to the other post.  The code you provided might change based on my other posts info.  Thanks again for any help you can provide.

 

 

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.