473,320 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Tricky explode(), help please!

Hello Everyone,

I have a situation that I just cannot wrap my head around. I would
appreciate any ideas or suggestions.

I am trying to parse and load a tab delimited file into a mysql
database. The file is generated by a scientific instrument. One file
contains the records of several people. Each person's information is
in 3 consecutive lines of the file.

A simplified demonstration:

id | test | result
1 | test1 | 50
| test 2 | 55
| test 3 | 60
2 | test 1 | 10
| test 2 | 15
| test 3 | 20
etc.

When I load this information into the database, it should have this
format
id | test 1 | test 2 | test 3
1 | 50 | 55 | 60
2 | 10 | 15 | 20

I'm okay with setting up the database, that's no problem.

I am however rather new to PHP and although I know I have to use the
explode() function to break this array down into its parts, I'm not
exactly sure how. Specifically, one file could contain the records of
up to 20 people, and I am not sure how I can loop through this file
(loaded into an array) in order to capture all of this info.

I hope I've given enough information to make sense. Again, any ideas,
examples or suggestions would be appreciated.

Thanks,
Andy

Feb 20 '06 #1
2 1148
to*******@yahoo.com wrote:
Hello Everyone,

I have a situation that I just cannot wrap my head around. I would
appreciate any ideas or suggestions.

I am trying to parse and load a tab delimited file into a mysql
database. The file is generated by a scientific instrument. One file
contains the records of several people. Each person's information is
in 3 consecutive lines of the file.

A simplified demonstration:

id | test | result
1 | test1 | 50
| test 2 | 55
| test 3 | 60
2 | test 1 | 10
| test 2 | 15
| test 3 | 20
etc.

When I load this information into the database, it should have this
format
id | test 1 | test 2 | test 3
1 | 50 | 55 | 60
2 | 10 | 15 | 20

I'm okay with setting up the database, that's no problem.

I am however rather new to PHP and although I know I have to use the
explode() function to break this array down into its parts, I'm not
exactly sure how. Specifically, one file could contain the records of
up to 20 people, and I am not sure how I can loop through this file
(loaded into an array) in order to capture all of this info.

I hope I've given enough information to make sense. Again, any ideas,
examples or suggestions would be appreciated.

Thanks,
Andy


Unless you know for sure that you will ALWAYS only have 3 tests then
your table design is flawed. If there can be more or less tests then
you want to use something like:

testid,testnum,testresult

It would take much less programming to insert this kind of data and then
you can use this cool "pivot table" example to retrieve the data- which
is identical to what you are trying to do...

See: http://en.wikibooks.org/wiki/Program...QL/Pivot_table

processing the array - NOT REAL SYNTAX - that is left to the programmer.

read file into array
process array
loopsstart
if ID is not emtpy or is null or = ''
(or whatever charaters are in the first position)
then
newid=row[0]
insertid=newid,insertestnum=row[1],result=row[2]
excute insert statement or write to an output
and load output file when all rows have been
processed
else
insertid=newid,insertestnum=row[1],result=row[2]

goto loopstart
Feb 20 '06 #2
to*******@yahoo.com wrote:
id | test | result
1 | test1 | 50
| test 2 | 55
| test 3 | 60
2 | test 1 | 10
| test 2 | 15
| test 3 | 20
etc.

When I load this information into the database, it should have this
format
id | test 1 | test 2 | test 3
1 | 50 | 55 | 60
2 | 10 | 15 | 20


$data = file('c:\data\200602\20_001.txt');
$flip = array();
$i = 0;
foreach ( $data as $line )
{
$a = explode("\t", rtrim($line));
if ( $a[0] ) $i = $a[0];
switch ( $a[1] )
{
case 'test 1': $j = 1; break;
case 'test 2': $j = 2; break;
case 'test 3': $j = 3; break;
default: $j = 99;
}
$flip[$i][$j] = $a[2];
}
$flat = array();
foreach ( $flip as $k => $v )
{
$v[0] = $k;
$flat[] = implode("\t", $v);
}

$fh = fopen('c:\data\200602\new_20_001.txt', 'wb');
fwrite($fh, implode("\r\n", $flat)."\r\n");
fclose($fh);

--
E. Dronkert
Feb 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: frizzle | last post by:
Hi there, i have a site with fake folders & files. htaccess rewrites everything to index.php?vars now in index.php i decide what file to include with a switch/case statement. to define where...
5
by: FFMG | last post by:
Hi, I need the php equivalent of explode in one of my app. I read a very big file and "explode" each line to fill a structure. The reading of the file, 19Mb, (I will also need to streamline...
0
by: k04jg02 | last post by:
Python has a nifty operator that will take a container and pass its elements as function parameters. In Python you can make a list like so: x = Then you can say: f(*x)
5
by: sathyashrayan | last post by:
Dear group, The function to be used as follows: $links = "http://www.campaignindia.in/feature/analysis"; $tag1 = '<div class=feature-wrapper>'; $tag2 = '<h1><a href'; $tag3 = "</a>"; $op =...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.