muppet77 Posted November 17, 2010 Share Posted November 17, 2010 i have a text file that has spaces between each column. eg 12 12 13 22 34.5 10 13 18 88 32.5 12 33 17 23 22.3 (the actual data is weather data that accumulates a new line every 15 mins and is seen at www.maidenerleghweather.com/clientraw.txt ) each column represents a weather variable. i would like to use php to find the maximum values in each column and minimum values etc etc. how can i get the text data into arrays, so that i can then easily perform some simple stats on it? HOWEVER each line would NOT be an array. the first value in each line makes up array number one, second makes up two etc any ideas for a newbie beginner would be welcomed. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/ Share on other sites More sharing options...
ignace Posted November 17, 2010 Share Posted November 17, 2010 $fopen = fopen('http://www.maidenerleghweather.com/clientraw.txt'); while($line = fgets($fopen, 16)) { list($nameit, $nameit2, $nameit3, $nameit4, $nameit5) = sscanf($line, '%d %d %d %d %f'); // process } Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1135685 Share on other sites More sharing options...
AbraCadaver Posted November 17, 2010 Share Posted November 17, 2010 With a fixed number of columns you could do this: $rows = file('http://www.maidenerleghweather.com/clientraw.txt'); foreach($rows as $row) { list($col[1][], $col[2][], $col[3][], $col[4][], $col[5][]) = explode(' ', $row); } Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1135713 Share on other sites More sharing options...
AbraCadaver Posted November 17, 2010 Share Posted November 17, 2010 Oh, and then probably sort(), reset() and end() on each column to get min and max. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1135717 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 perfect! how would i then interrogate the data and find out some stats?? print max($col[3]); print count($col[3]); print min($col[3]); ??? Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1135951 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 .........and is there a way i can find a value in array[0] - ie the time, if i know a value in array[3]? eg if i find the max temperature in one column, can i look up the date and time that it occurred at? also, is it possible to lookup values with conditional if functions? so for example if i had the month in a column, could i look up the maximum temp array[3] when the month = november? thanks for your help guys, i hope to be this good someday! Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1135955 Share on other sites More sharing options...
ignace Posted November 18, 2010 Share Posted November 18, 2010 perfect! how would i then interrogate the data and find out some stats?? print max($col[3]); print count($col[3]); print min($col[3]); ??? Yes eg if i find the max temperature in one column, can i look up the date and time that it occurred at? You can't retrieve data that isn't send to you by the weather station. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1135956 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 perfect! how would i then interrogate the data and find out some stats?? print max($col[3]); print count($col[3]); print min($col[3]); ??? Yes eg if i find the max temperature in one column, can i look up the date and time that it occurred at? You can't retrieve data that isn't send to you by the weather station. no...sorry.... in http://www.maidenerleghweather.com/clientraw.txt the first column is date and the second column is time. this means that array[0] is date and [1] is time, right? i have the script running at http://www.maidenerleghweather.com/1atest.php which currently displays the max temp recorded (7.4) can i lookup the date and time from col 1 and 2? Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1135959 Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 It's going to be much easier to just dump this into a database, MySQL or SQLite. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136147 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 any pointers mate? Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136161 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 well this is what i have so far and i have worked out the function i need to grab the time and date of the record. <?php $rows = file('http://www.maidenerleghweather.com/clientraw.txt');foreach($rows as $row) { list($col[0][], $col[1][], $col[2][], $col[3][], $col[4][]) = explode(' ', $row);} $count2 = count($col[2]); $dat = max($col[0]); $tim = end($col[1]); $tempmax = max($col[2]); $tempmaxpos = array_search($tempmax,$col[2]); $tempmaxdate = $col[0][$tempmaxpos]; $tempmaxtime = $col[1][$tempmaxpos]; $tempmin = min($col[2]); $tempminpos = array_search($tempmin,$col[2]); $tempmindate = $col[0][$tempminpos]; $tempmintime = $col[1][$tempminpos]; print " max temp $tempmax @ $tempmaxtime on $tempmaxdate <br /> min temp $tempmin @ $tempmintime on $tempmindate <br /> last update sent @ $tim on $dat<br /> $count2 records"; ?> all i need now is to work out how to do conditional searches. eg max temp when date = november for example. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136246 Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 any pointers mate? The problem is that your data is not consistent. Many lines have 31 or 32 columns and several have 29 or 30. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136256 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 yes that's as i am fine tuning the scraping - i am just 'paving the way' ahead to see if it is do-able and hence worthwhile. do you think i need a database - the php stuff seems ok for me? Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136265 Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 yes that's as i am fine tuning the scraping - i am just 'paving the way' ahead to see if it is do-able and hence worthwhile. do you think i need a database - the php stuff seems ok for me? First, see if you have database extensions loaded and I'll give you an example. Use phpinfo() or just try the functions and see if you get a fatal error: mysql_connect(); //and sqlite_open(); Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136273 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 have a look at this http://www.maidenerleghweather.com/11aa.php Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136278 Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 Cool. Here is an example. Use the sqlite.org site for help with functions etc... This will create a new table every time: // name these whatever you want after you have decided how many columns there will be $columns = array('column1', 'column2', 'column3', 'column4', 'column5', 'column6', 'column7', 'column8', 'column9', 'column10', 'column11', 'column12', 'column13', 'column14', 'column15', 'column16', 'column17', 'column18', 'column19', 'column20', 'column21', 'column22', 'column23', 'column24', 'column25', 'column26', 'column27', 'column28', 'column29', 'column30', 'column31', 'column32'); // open or create db, webserver must have write perms to directory where script resides, or try /tmp $con = sqlite_open('client.db'); // delete the table $res = sqlite_query($con, "DROP TABLE stats"); // create the table with the defined columns $res = sqlite_query($con, "CREATE TABLE stats ( " . implode(',', $columns) . " )"); // read file into array of lines (rows) $lines = file('http://www.maidenerleghweather.com/clientraw.txt', FILE_IGNORE_NEW_LINES); foreach($lines as $i => $line) { // split line into columns $data = explode(' ', $line); // remove date (first column) from array $thedate = array_shift($data); // replace / with - so strtotime will work and add time (first column) to date $datetime = str_replace('/', '-', $thedate) . " " . $data[0]; // put datetime in a more standard format and assign to first column (replacing time) $data[0] = date('Y-m-d G:i:s', strtotime($datetime)); // make sure data column count is what is expected if(count($data) == count($columns)) { // insert data into db $res = sqlite_query($con, "INSERT INTO stats ( " . implode(',', $columns) . " ) VALUES ( " . "'" . implode("','", $data) . "' )"); } else { echo "Line " . $i++ . ": " . count($data) . " columns, " . count($columns) . " expected!<br />\n"; } } // then just use standard SQL to get what you want, max of some column where month is November $res = sqlite_query($con, "SELECT MAX(column2) AS high FROM stats WHERE strftime('%m', column1) = 11"); $rows = sqlite_fetch_all($res, SQLITE_ASSOC); Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136300 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 so i just put this in a page and save it as php? sorry - a complete novice. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136311 Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 so i just put this in a page and save it as php? sorry - a complete novice. Yep, try it. Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136314 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 how do i copy and paste it without getting lines disjointed and wrong? tried it and it fails? Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136317 Share on other sites More sharing options...
muppet77 Posted November 18, 2010 Author Share Posted November 18, 2010 ah.getting warmer. i now get http://www.maidenerleghweather.com/recordtest.php Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136322 Share on other sites More sharing options...
AbraCadaver Posted November 18, 2010 Share Posted November 18, 2010 ah.getting warmer. i now get http://www.maidenerleghweather.com/recordtest.php OK, last example an then you have to hit some tutorials: echo $rows[0]['high']; Quote Link to comment https://forums.phpfreaks.com/topic/218990-text-file-data-to-arrays/#findComment-1136328 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.