php - Undefined Offset 1 on fgetcsv -
the following url contains tab-separated table of data particular day. 1 below january 4, 2011.
http://lpo.dt.navy.mil/data/dm/2011/2011_01_04/air_temp
i wrote script go through entire date range of 2010-06-01 today, grabbing date , air temperature value , insert database. amount of data kept causing timeout weird miracle able in 2014-03-27. here's script used (probably not elegant solution i'm staring out).
public function requestdata(){ $year = 2014; $startdate = new datetime('2014-03-28'); $enddate = new datetime('2014-05-05'); $incrementday = new dateinterval('p1d'); while ($startdate <= $enddate) { $results = []; $startdateformat = $startdate->format('y-m-d'); //format datetime object $startdateurl = str_replace( "-", "_", $startdateformat); //prep date url entry $dayofyear = date("z", strtotime($startdateformat)); //store day of year if ($dayofyear == '0') //if day of year 0, increment year 1 { $year++; } $url = 'http://lpo.dt.navy.mil/data/dm/' . $year . '/' . $startdateurl . '/air_temp'; $url_header = @get_headers($url); //if url found, open url, if not found set empty $file = ($url_header[0] == 'http/1.1 200 ok') ? fopen($url, 'r') : "empty"; //if file not found, skip iteration of loop, increment day , return top of loop if ($file == "empty") { $startdate->add($incrementday); continue; } while (($line = fgetcsv($file, 500, "\t")) !== false) { $dataarray = explode(" ", $line[0]); $date = substr($dataarray[0], 0, 10); $moddate = str_replace("_", "-", $date); // var_dump($dataarray); $results[$moddate]['air'][] = $dataarray[1]; } fclose($file); foreach ($results $date => $subdata) { db::table('weatherdata')->insert(array('date' => $date)); db::table('weatherdata')->where('date', $date)->update(array('air_temp' => implode(",", $subdata['air']))); } $startdate->add($incrementday);
i able upload data before, it's giving me undefined offset error , i'm not sure why. var_dumped $dataarray , dumps out each array this:
array (size=2) 0 => string '2014_03_28 13:14:57' (length=19) 1 => string '38.4' (length=4) array (size=2) 0 => string '2014_03_28 13:15:57' (length=19) 1 => string '38.4' (length=4) ...and on , forth
it seems defined, i'm not sure why i'm getting error. appreciated.
Comments
Post a Comment