473,320 Members | 1,950 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.

Array to hold multiple file content

Hi All,

I have numerous files that look like the chunk below. From the date and
time I calculate a year day value. For the example that value is
148.67721064815

I'm not an expert yet at dealing with arrays, but I get around well in
php otherwise (reading data files, loops, functions, etc).

My question is how would I go about creating an array that would hold
the year day as a key and the entire file contents in the next element?
Then when I advance to the next file do the same thing so that my ending
array would come out something like this;

[yearday file1][file1 contents]
[yearday file2][file2 contents]
[yearday file3][file3 contents]
....
...
..

Or is what I am wanting to do out of the question?

Appreciate your help.

Patrick

File example;

Descent 27.4279 -83.0965 05-29-06 16:15:11 55.052 26.52 0.95 35.28
Descent 27.4279 -83.0965 05-29-06 16:15:11 55.014 26.51 1.95 35.26
Descent 27.4279 -83.0965 05-29-06 16:15:11 55.047 26.50 3.04 35.29
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.957 26.47 4.29 35.24
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.947 26.45 5.33 35.25
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.871 26.41 6.42 35.22
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.875 26.36 7.52 35.25
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.884 26.30 8.65 35.31
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.887 26.24 9.78 35.35
--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group – USF - College of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld
Sep 19 '06 #1
2 1503
Hi Patrick,

Using php's associative arrays, you can map a key, which in your case
is the result value you calculated, to a value, which would be the file
contents. For example:

$results[strvalue($yearDayValue)] = file_get_contents('file.dat');

The strvalue function is used to convert the float into a string,
because floats are truncated to integers when used directly as keys in
an associative array.

Here's a more extended usage example:

<?

function getResults()
{
$results = array(); // Results array

// Get all *.data files in the current dir
$files = glob('*.data');

foreach ($files as $file) {
// Read the contents of the file
$contents = file_get_contents($file);
// Call your function to calculate the result
$result = getMyResult($contents);
// Store the result. Convert the floating point key
// into a string because float keys are truncated to
// integers when used directly.
$results[strval($result)] = $contents;
}

// Now $results maps the individual result to each
// file's contents

print_r($results);
echo "\n";

echo "There are " . count($results) . " results.\n\n";

// Iterate over the results

foreach ($results as $key =$value) {
echo "The year-day value is $key and the file contains:\n\n" .
"$value\n";
}
}

function getMyResult($contents)
{
$result = 0;
$lines = explode("\n", $contents);
foreach ($lines as $line) {
if ($line != '') {
list($descent, $a, $b, $date, $time, $c, $d, $e, $f) =
explode(' ', $line);
$result += $f;
}
}
return $result;
}

getResults();

?>

Best Regards,

John Peters

Patrick wrote:
Hi All,

I have numerous files that look like the chunk below. From the date and
time I calculate a year day value. For the example that value is
148.67721064815

I'm not an expert yet at dealing with arrays, but I get around well in
php otherwise (reading data files, loops, functions, etc).

My question is how would I go about creating an array that would hold
the year day as a key and the entire file contents in the next element?
Then when I advance to the next file do the same thing so that my ending
array would come out something like this;

[yearday file1][file1 contents]
[yearday file2][file2 contents]
[yearday file3][file3 contents]
...
..
.

Or is what I am wanting to do out of the question?

Appreciate your help.

Patrick

File example;

Descent 27.4279 -83.0965 05-29-06 16:15:11 55.052 26.52 0.95 35.28
Descent 27.4279 -83.0965 05-29-06 16:15:11 55.014 26.51 1.95 35.26
Descent 27.4279 -83.0965 05-29-06 16:15:11 55.047 26.50 3.04 35.29
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.957 26.47 4.29 35.24
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.947 26.45 5.33 35.25
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.871 26.41 6.42 35.22
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.875 26.36 7.52 35.25
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.884 26.30 8.65 35.31
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.887 26.24 9.78 35.35
--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group - USF - College of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld
Sep 19 '06 #2
Thanks John,

I will take a look at this and see what I can work out.

Patrick

pe*******@gmail.com wrote:
Hi Patrick,

Using php's associative arrays, you can map a key, which in your case
is the result value you calculated, to a value, which would be the file
contents. For example:

$results[strvalue($yearDayValue)] = file_get_contents('file.dat');

The strvalue function is used to convert the float into a string,
because floats are truncated to integers when used directly as keys in
an associative array.

Here's a more extended usage example:

<?

function getResults()
{
$results = array(); // Results array

// Get all *.data files in the current dir
$files = glob('*.data');

foreach ($files as $file) {
// Read the contents of the file
$contents = file_get_contents($file);
// Call your function to calculate the result
$result = getMyResult($contents);
// Store the result. Convert the floating point key
// into a string because float keys are truncated to
// integers when used directly.
$results[strval($result)] = $contents;
}

// Now $results maps the individual result to each
// file's contents

print_r($results);
echo "\n";

echo "There are " . count($results) . " results.\n\n";

// Iterate over the results

foreach ($results as $key =$value) {
echo "The year-day value is $key and the file contains:\n\n" .
"$value\n";
}
}

function getMyResult($contents)
{
$result = 0;
$lines = explode("\n", $contents);
foreach ($lines as $line) {
if ($line != '') {
list($descent, $a, $b, $date, $time, $c, $d, $e, $f) =
explode(' ', $line);
$result += $f;
}
}
return $result;
}

getResults();

?>

Best Regards,

John Peters

Patrick wrote:
>>Hi All,

I have numerous files that look like the chunk below. From the date and
time I calculate a year day value. For the example that value is
148.67721064815

I'm not an expert yet at dealing with arrays, but I get around well in
php otherwise (reading data files, loops, functions, etc).

My question is how would I go about creating an array that would hold
the year day as a key and the entire file contents in the next element?
Then when I advance to the next file do the same thing so that my ending
array would come out something like this;

[yearday file1][file1 contents]
[yearday file2][file2 contents]
[yearday file3][file3 contents]
...
..
.

Or is what I am wanting to do out of the question?

Appreciate your help.

Patrick

File example;

Descent 27.4279 -83.0965 05-29-06 16:15:11 55.052 26.52 0.95 35.28
Descent 27.4279 -83.0965 05-29-06 16:15:11 55.014 26.51 1.95 35.26
Descent 27.4279 -83.0965 05-29-06 16:15:11 55.047 26.50 3.04 35.29
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.957 26.47 4.29 35.24
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.947 26.45 5.33 35.25
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.871 26.41 6.42 35.22
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.875 26.36 7.52 35.25
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.884 26.30 8.65 35.31
Descent 27.4279 -83.0965 05-29-06 16:15:11 54.887 26.24 9.78 35.35
--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group - USF - College of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld


--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group – USF - College of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld

Sep 19 '06 #3

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

Similar topics

2
by: Nick | last post by:
Im trying to build an array from elements I have stored in a text file (So I can access them on multiple pages). What is the syntax for this? I thought it might be... storedArray1 =...
4
by: Michael Kirchner | last post by:
Hi everybody The output of my multiple dimension array is quite confusing. Im declaring an array, store some values in it and then I save the array in a session variable. On an other page I...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
5
by: jpaterso | last post by:
Here's the code. When I run it, the array prints fine in the while loop but I get the last person in every array element in the for loop. Thanks in advance. File: Jerry,12 Lon,11
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
15
by: mdh | last post by:
May I ask. If an array is defined , not as a static, but outside of a function, is there any guarantee as to the contents of each element? Thanks.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
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.